fix missing bold for your account item
[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 'asta*.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         rex=''
87         ls -Sd $( imglist_fla "$abspa" "$2" "$rex" "y" ) | grep -v '^\.$'
88         ls -Sd $( imglist_fla "$abspa" "$2" "$rex" "n" ) | grep -v '^\.$'
89     fi
90 }
91
92 for lang in it en; do
93     if [ "$lang" = "it" ]; then
94         fsuf=""
95     else
96         fsuf="-${lang}"
97     fi
98
99     for dpath in "" briskin5/ ; do
100         OUTFILE="web/${dpath}"preload_img${fsuf}.js
101         echo "creating $OUTFILE ..."
102         rm -f $OUTFILE
103         IMGPATH="${IMGPATHBASE}${dpath}img"
104         (
105             echo "var g_preload_img_arr = new Array( "
106             first=1
107             spa="            "
108             ltri="$(echo "$IMGPATH" | wc -c)"
109             for i in $(imglist "$dpath" "$lang"); do
110                 if [ $first -ne 1 ]; then
111                     echo -n ","
112                     if [ $((ct % 2)) -eq 0 ]; then
113                         echo
114                         echo -n "$spa"
115                     fi
116                 else
117                     echo -n "$spa"
118                 fi
119                 outna="img/$(echo "$i" | cut -c $((ltri + 1))-)"
120                 if [ $((ct % 2)) -eq 0 ]; then
121                     echo -n "\"$outna\""
122                 else
123                     echo -n " \"$outna\""
124                 fi
125                 ct=$((ct + 1))
126                 first=0
127             done
128             echo "CT: $ct" >&2
129             echo ");"
130         ) >> $OUTFILE
131         
132         (
133             echo "var g_preload_imgsz_arr = new Array( "
134             first=1
135             sum=0
136             spa="            "
137             tot=0
138             ltri="$(echo "$IMGPATH" | wc -c)"
139             for i in $(imglist "$dpath" "$lang"); do
140                 outna="$(echo "$i" | cut -c $((ltri + 1))-)"
141                 sz="$(stat -c '%s' $IMGPATH/$outna)"
142                 tot=$((tot + sz))
143             done
144             
145             for i in $(imglist "$dpath" "$lang"); do
146                 outna="$(echo "$i" | cut -c $((ltri + 1))-)"
147                 if [ $first -ne 1 ]; then
148                     echo -n ","
149                     if [ $((ct % 8)) -eq 0 ]; then
150                         echo
151                         echo -n "$spa"
152                     fi
153                 else
154                     echo -n "$spa"
155                 fi
156                 sz="$(stat -c '%s' $IMGPATH/$outna)"
157                 sum=$((sum + sz))
158                 cur="$(echo "100.0 * $sum / $tot" | bc -l | sed 's/\(\.[0-9]\)[0-9]*/\1/g')"
159                 if [ $((ct % 8)) -eq 0 ]; then
160                     echo -n "\"$cur\""
161                 else
162                     echo -n " \"$cur\""
163                 fi
164                 ct=$((ct + 1))
165                 first=0
166             done
167             
168             echo "CT2: $ct" >&2
169
170             echo ");"
171         ) >> $OUTFILE
172     done
173 done
174 exit 0