Copyright date updated
[brisk.git] / bin / preload.sh
1 #!/bin/bash
2 #
3 #  brisk - preload.sh
4 #
5 #  Copyright (C) 2011-2012 Matteo Nastasi
6 #                          mailto: nastasi@alternativeoutput.it
7 #                                  matteo.nastasi@milug.org
8 #                          web: http://www.alternativeoutput.it
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details. You should have received a
19 # copy of the GNU General Public License along with this program; if
20 # not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 # Suite 330, Boston, MA 02111-1307, USA.
22 #
23 #
24
25 IMGPATHBASE="../brisk-img/"
26
27 set -x
28
29 # (
30 # echo '<?php'
31 # echo 'header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1'
32 # echo 'header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past'
33 # echo '?>'
34 # ) > $OUTFILE
35
36 function imglist_fla () {
37     local pname lang
38     pname="$1"
39     lang="$2"
40     rex="$3"
41     wrex="$4"
42     ret=""
43     for i in $(find "$pname" -maxdepth 1 -type f -name '*.jpg' -o -name '*.png' -o -name '*.gif' | grep -v '/src_' | sort); do
44         if [ "$rex" != "" ]; then
45             echo "$i" | grep -q "$rex"
46             rt=$?
47             if [ "$wrex" = "y" -a $rt -ne 0  ]; then
48                 continue
49             fi
50             if [ "$wrex" = "n" -a $rt -eq 0  ]; then
51                 continue
52             fi
53         fi
54         echo "$i" | grep -q '.*\-[a-z][a-z]\....$'
55         if [ $? -eq 0 ]; then
56             # se file con suffisso di lingua
57             suff="$(echo "$i" | sed 's/\(.*\)\-\([a-z][a-z]\)\.\(...\)$/\2/g')"
58
59             if [ "$lang" = "$suff" ]; then
60                 echo "$i"
61             fi
62         else
63             eni="$(echo "$i" | sed 's/\(.*\)\.\(...\)$/\1-en.\2/g')"
64             if [ -f $eni ]; then
65                 # esiste la versione _en
66                 if [ "$lang" = "it" ]; then
67                     # se lingua italiana le img mlang nn hanno estensione quindi va presa
68                     echo "$i"
69                 fi
70             else
71                 # NON esiste la versione _en quindi e' una immagine NON mlang e va comunque presa
72                 echo "$i"
73             fi
74         fi
75     done 
76 }
77
78 function imglist () {
79     local abspa
80     abspa="${IMGPATHBASE}${1}img"
81     if [ "$1" = "" ]; then
82         ls -S $( imglist_fla "$abspa" "$2" )
83     elif [ "$1" = "briskin5/" ]; then
84         rex='/[0-9][0-9][^/]*$'
85         ls -S $( imglist_fla "$abspa" "$2" "$rex" "y" )
86         ls -S $( imglist_fla "$abspa" "$2" "$rex" "n" )
87     fi
88 }
89
90 for lang in it en; do
91     if [ "$lang" = "it" ]; then
92         fsuf=""
93     else
94         fsuf="-${lang}"
95     fi
96
97     for dpath in "" briskin5/ ; do
98         OUTFILE="web/${dpath}"preload_img${fsuf}.js
99         echo "creating $OUTFILE ..."
100         rm -f $OUTFILE
101         IMGPATH="${IMGPATHBASE}${dpath}img"
102         (
103             echo "var g_preload_img_arr = new Array( "
104             first=1
105             spa="            "
106             ltri="`echo "$IMGPATH" | wc -c`"
107             for i in $(imglist "$dpath" "$lang"); do
108                 if [ $first -ne 1 ]; then
109                     echo -n ", "
110                     if [ $((ct % 2)) -eq 0 ]; then
111                         echo
112                         echo -n "$spa"
113                     fi
114                 else
115                     echo -n "$spa"
116                 fi
117                 outna="img/`echo "$i" | cut -c $((ltri + 1))-`"
118                 echo -n "\"$outna\""
119                 ct=$((ct + 1))
120                 first=0
121             done
122             echo "CT: $ct" >&2
123             echo ");"
124         ) >> $OUTFILE
125         
126         (
127             echo "var g_preload_imgsz_arr = new Array( "
128             first=1
129             sum=0
130             spa="            "
131             tot=0
132             ltri="`echo "$IMGPATH" | wc -c`"
133             for i in $(imglist "$dpath" "$lang"); do
134                 outna="`echo "$i" | cut -c $((ltri + 1))-`"
135                 sz="`stat -c '%s' $IMGPATH/$outna`"
136                 tot=$((tot + sz))
137             done
138             
139             for i in $(imglist "$dpath" "$lang"); do
140                 outna="`echo "$i" | cut -c $((ltri + 1))-`"
141                 if [ $first -ne 1 ]; then
142                     echo -n ", "
143                     if [ $((ct % 8)) -eq 0 ]; then
144                         echo
145                         echo -n "$spa"
146                     fi
147                 else
148                     echo -n "$spa"
149                 fi
150                 sz="`stat -c '%s' $IMGPATH/$outna`"
151                 sum=$((sum + sz))
152                 cur="`echo "100.0 * $sum / $tot" | bc -l | sed 's/\(\.[0-9]\)[0-9]*/\1/g'`"
153                 echo -n "\"$cur\""
154                 ct=$((ct + 1))
155                 first=0
156             done
157             
158             echo "CT2: $ct" >&2
159             
160             echo ");"
161         ) >> $OUTFILE
162     done
163 done
164 exit 0