Update.
[scripts.git] / dhtml.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 set -e
21 set -o pipefail
22
23 function hsize () {
24     size=$1
25
26     if [[ ${size} -lt 1024 ]]
27     then
28         size_h=${size}b
29     elif [[ ${size} -lt 1048576 ]]
30     then
31         size_h=$(bc <<<"scale=1;${size} / 1024.0")Kb
32     elif [[ ${size} -lt 1073741824 ]]
33     then
34         size_h=$(bc <<<"scale=1;${size} / (1024.0*1024.0)")Mb
35     else
36         size_h=$(bc <<<"scale=2;${size} / (1024.0*1024.0*1024.0)")Gb
37     fi
38
39     echo ${size_h}
40 }
41
42 while read line
43 do
44
45     while [[ ${line} =~ @DATE ]]
46     do
47         DATE=$(date)
48         line=$(sed -e "s/@DATE/${DATE}/" <<<"${line}")
49     done
50
51     while [[ ${line} =~ @EXEC ]]
52     do
53         command=$(sed -e "s/^.*@EXEC{\([^}]*\)}.*$/\1/" <<<"${line}")
54         prefix=$(sed -e "s/\(^.*\)@EXEC.*$/\1/" <<<"${line}")
55         suffix=$(sed -e "s/^.*@EXEC{[^}]*}\(.*\)$/\1/" <<<"${line}")
56         line=${prefix}$(${command})$suffix
57     done
58
59     while [[ ${line} =~ @SIZE ]]
60     do
61         filename=$(sed -e "s/^.*@SIZE{\([^}]*\)}.*$/\1/" <<<"${line}")
62         if [[ ${filename} =~ ^http ]]
63         then
64             filesize=$(curl -LsI "${filename}" | grep -i ^Content-Length: | sed -e 's/^[^:]*: \([0-9]*\).*$/\1/')
65         else
66             filesize=$(stat -c %s "${filename}")
67         fi
68         filesize_h=$(hsize ${filesize})
69         line=$(sed -e "s/@SIZE{\([^}]*\)}/${filesize_h}/" <<<"${line}")
70     done
71
72     while [[ ${line} =~ @URLDATE ]]
73     do
74         filename=$(sed -e "s/^.*@URLDATE{\([^}]*\)}.*$/\1/" <<<"${line}")
75         timestamp=$(curl -Lv "${filename}" 2>&1 | \grep '^< Last-Modified:' | sed -e 's/^.*Last-Modified: *//')
76         timestamp=$(date -d "${timestamp}" +'%b %e, %Y')
77         line=$(sed -e "s/@URLDATE{\([^}]*\)}/${timestamp}/" <<<"${line}")
78     done
79
80     while [[ ${line} =~ @FILEDATE ]]
81     do
82         filename=$(sed -e "s/^.*@FILEDATE{\([^}]*\)}.*$/\1/" <<<"${line}")
83         filedate=$(date +'%b %e, %Y' -r "${filename}")
84         line=$(sed -e "s/@FILEDATE{\([^}]*\)}/${filedate}/" <<<"${line}")
85     done
86
87     echo "${line}"
88 done