more robust sql scripts adding IF EXISTS to DROP TABLE statementes
[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     for i in $(find "$pname" -maxdepth 1 -type f -name '[0-9][0-9]*.png' -o -name 'st_*.png' | grep -v '/src_' | sort); do
45         if [ "$rex" != "" ]; then
46             echo "$i" | grep -q "$rex"
47             rt=$?
48             if [ "$wrex" = "y" -a $rt -ne 0  ]; then
49                 continue
50             fi
51             if [ "$wrex" = "n" -a $rt -eq 0  ]; then
52                 continue
53             fi
54         fi
55         echo "$i" | grep -q '.*\-[a-z][a-z]\....$'
56         if [ $? -eq 0 ]; then
57             # se file con suffisso di lingua
58             suff="$(echo "$i" | sed 's/\(.*\)\-\([a-z][a-z]\)\.\(...\)$/\2/g')"
59
60             if [ "$lang" = "$suff" ]; then
61                 echo "$i"
62             fi
63         else
64             eni="$(echo "$i" | sed 's/\(.*\)\.\(...\)$/\1-en.\2/g')"
65             if [ -f $eni ]; then
66                 # esiste la versione _en
67                 if [ "$lang" = "it" ]; then
68                     # se lingua italiana le img mlang nn hanno estensione quindi va presa
69                     echo "$i"
70                 fi
71             else
72                 # NON esiste la versione _en quindi e' una immagine NON mlang e va comunque presa
73                 echo "$i"
74             fi
75         fi
76     done 
77 }
78
79 function imglist () {
80     local abspa
81     abspa="${IMGPATHBASE}${1}img"
82     if [ "$1" = "" ]; then
83         ls -Sd $( imglist_fla "$abspa" "$2" ) | grep -v '^\.$'
84     elif [ "$1" = "briskin5/" ]; then
85         rex='/[0-9][0-9][^/]*$'
86         ls -Sd $( imglist_fla "$abspa" "$2" "$rex" "y" ) | grep -v '^\.$'
87         ls -Sd $( imglist_fla "$abspa" "$2" "$rex" "n" ) | grep -v '^\.$'
88     fi
89 }
90
91 for lang in it en; do
92     if [ "$lang" = "it" ]; then
93         fsuf=""
94     else
95         fsuf="-${lang}"
96     fi
97
98     for dpath in "" briskin5/ ; do
99         OUTFILE="web/${dpath}"preload_img${fsuf}.js
100         echo "creating $OUTFILE ..."
101         rm -f $OUTFILE
102         IMGPATH="${IMGPATHBASE}${dpath}img"
103         (
104             echo "var g_preload_img_arr = new Array( "
105             first=1
106             spa="            "
107             ltri="$(echo "$IMGPATH" | wc -c)"
108             for i in $(imglist "$dpath" "$lang"); do
109                 if [ $first -ne 1 ]; then
110                     echo -n ","
111                     if [ $((ct % 2)) -eq 0 ]; then
112                         echo
113                         echo -n "$spa"
114                     fi
115                 else
116                     echo -n "$spa"
117                 fi
118                 outna="img/$(echo "$i" | cut -c $((ltri + 1))-)"
119                 if [ $((ct % 2)) -eq 0 ]; then
120                     echo -n "\"$outna\""
121                 else
122                     echo -n " \"$outna\""
123                 fi
124                 ct=$((ct + 1))
125                 first=0
126             done
127             echo "CT: $ct" >&2
128             echo ");"
129         ) >> $OUTFILE
130         
131         (
132             echo "var g_preload_imgsz_arr = new Array( "
133             first=1
134             sum=0
135             spa="            "
136             tot=0
137             ltri="$(echo "$IMGPATH" | wc -c)"
138             for i in $(imglist "$dpath" "$lang"); do
139                 outna="$(echo "$i" | cut -c $((ltri + 1))-)"
140                 sz="$(stat -c '%s' $IMGPATH/$outna)"
141                 tot=$((tot + sz))
142             done
143             
144             for i in $(imglist "$dpath" "$lang"); do
145                 outna="$(echo "$i" | cut -c $((ltri + 1))-)"
146                 if [ $first -ne 1 ]; then
147                     echo -n ","
148                     if [ $((ct % 8)) -eq 0 ]; then
149                         echo
150                         echo -n "$spa"
151                     fi
152                 else
153                     echo -n "$spa"
154                 fi
155                 sz="$(stat -c '%s' $IMGPATH/$outna)"
156                 sum=$((sum + sz))
157                 cur="$(echo "100.0 * $sum / $tot" | bc -l | sed 's/\(\.[0-9]\)[0-9]*/\1/g')"
158                 if [ $((ct % 8)) -eq 0 ]; then
159                     echo -n "\"$cur\""
160                 else
161                     echo -n " \"$cur\""
162                 fi
163                 ct=$((ct + 1))
164                 first=0
165             done
166             
167             echo "CT2: $ct" >&2
168
169             echo ");"
170         ) >> $OUTFILE
171     done
172 done
173 exit 0