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