Initial commit.
authorFrancois Fleuret <francois@fleuret.org>
Mon, 8 Feb 2021 10:06:32 +0000 (11:06 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Mon, 8 Feb 2021 10:06:32 +0000 (11:06 +0100)
dhtml.sh [new file with mode: 0755]

diff --git a/dhtml.sh b/dhtml.sh
new file mode 100755 (executable)
index 0000000..8b74fe2
--- /dev/null
+++ b/dhtml.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+#########################################################################
+# This program is free software: you can redistribute it and/or modify  #
+# it under the terms of the version 3 of the GNU General Public License #
+# as published by the Free Software Foundation.                         #
+#                                                                       #
+# This program is distributed in the hope that it will be useful, but   #
+# WITHOUT ANY WARRANTY; without even the implied warranty of            #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
+# General Public License for more details.                              #
+#                                                                       #
+# You should have received a copy of the GNU General Public License     #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.  #
+#                                                                       #
+# Written by and Copyright (C) Francois Fleuret                         #
+# Contact <francois@fleuret.org> for comments & bug reports             #
+#########################################################################
+
+set -e
+set -o pipefail
+
+function hsize () {
+    size=$1
+
+    if [[ ${size} -lt 1024 ]]
+    then
+        size_h=${size}b
+    elif [[ ${size} -lt 1048576 ]]
+    then
+        size_h=$(bc <<<"scale=1;${size} / 1024.0")Kb
+    elif [[ ${size} -lt 1073741824 ]]
+    then
+        size_h=$(bc <<<"scale=1;${size} / (1024.0*1024.0)")Mb
+    else
+        size_h=$(bc <<<"scale=2;${size} / (1024.0*1024.0*1024.0)")Gb
+    fi
+
+    echo ${size_h}
+}
+
+while read line
+do
+
+    while [[ ${line} =~ @DATE ]]
+    do
+        DATE=$(date)
+        line=$(sed -e "s/@DATE/${DATE}/" <<<"${line}")
+    done
+
+    while [[ ${line} =~ @EXEC ]]
+    do
+        command=$(sed -e "s/^.*@EXEC{\([^}]*\)}.*$/\1/" <<<"${line}")
+        prefix=$(sed -e "s/\(^.*\)@EXEC.*$/\1/" <<<"${line}")
+        suffix=$(sed -e "s/^.*@EXEC{[^}]*}\(.*\)$/\1/" <<<"${line}")
+        line=${prefix}$(${command})$suffix
+    done
+
+    while [[ ${line} =~ @SIZE ]]
+    do
+        filename=$(sed -e "s/^.*@SIZE{\([^}]*\)}.*$/\1/" <<<"${line}")
+        if [[ ${filename} =~ ^http ]]
+        then
+            filesize=$(curl -LsI "${filename}" | grep -i ^Content-Length: | sed -e 's/^[^:]*: \([0-9]*\).*$/\1/')
+        else
+            filesize=$(stat -c %s "${filename}")
+        fi
+        filesize_h=$(hsize ${filesize})
+        line=$(sed -e "s/@SIZE{\([^}]*\)}/${filesize_h}/" <<<"${line}")
+    done
+
+    while [[ ${line} =~ @FILEDATE ]]
+    do
+        filename=$(sed -e "s/^.*@FILEDATE{\([^}]*\)}.*$/\1/" <<<"${line}")
+        filedate=$(date +'%b %e, %Y' -r "${filename}")
+        line=$(sed -e "s/@FILEDATE{\([^}]*\)}/${filedate}/" <<<"${line}")
+    done
+
+    echo "${line}"
+done