wok view tramys-server/stuff/tramys2.cgi @ rev 17086

tramys-client, tramys-server: now using http headers only (no more additional info in the server logs); added a lot of comments.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Aug 26 13:31:50 2014 +0300 (2014-08-26)
parents 1794963d7994
children bf52d3ac4b95
line source
1 #!/bin/sh
2 # tramys - TRAnslate MY Slitaz. Server solution
3 # Tool for managing translation files for SliTaz GNU/Linux
4 # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
6 # How to use:
7 # 1. Request for archive:
8 # HTTP_ACCEPT_LANGUAGE -> users locale
9 # HTTP_ACCEPT -> SliTaz release
10 # HTTP_COOKIE (list=...) -> space-separated list of packages to process
11 #
12 # 2. Remove archive that the user has refused to download:
13 # HTTP_COOKIE (rm=DLKEY) -> remove /tmp/tmp.DLKEY.tgz file
14 #
15 # 3. Send archive to user:
16 # HTTP_COOKIE (dl=DLKEY) -> send /tmp/tmp.DLKEY.tgz file
18 . /usr/bin/httpd_helper.sh
20 WORKING=$(busybox mktemp -d) # make temp working dir /tmp/tmp.??????
21 DATADIR=/usr/share/tramys # this folder contains lists
23 # Get user settings from HTTP headers.
24 lang="$HTTP_ACCEPT_LANGUAGE"
25 rel="$HTTP_ACCEPT"
26 cmd="${HTTP_COOKIE%%=*}"
27 arg="${HTTP_COOKIE#*=}"
29 #-----------#
30 # Functions #
31 #-----------#
33 # Prepare list for search.
34 # Original GNU gettext searches precisely in this order.
35 locales_list() {
36 LL=$(echo $1 | sed 's|^\([^_.@]*\).*$|\1|')
37 CC=$(echo $1 | sed -n '/_/s|^[^_]*\(_[^.@]*\).*$|\1|p')
38 EE=$(echo $1 | sed -n '/./s|^[^\.]*\(\.[^@]*\).*$|\1|p')
39 VV=$(echo $1 | sed -n '/@/s|^[^@]*\(@.*\)$|\1|p')
40 ee=$(echo $EE | tr A-Z a-z | tr -cd a-z0-9); [ "$ee" ] && ee=.$ee
41 [ "x$EE" == "x$ee" ] && ee=''
43 [ "$CC" -a "$EE" -a "$VV" ] && echo -n "$LL$CC$EE$VV "
44 [ "$CC" -a "$ee" -a "$VV" ] && echo -n "$LL$CC$ee$VV "
45 [ "$CC" -a "$VV" ] && echo -n "$LL$CC$VV "
46 [ "$EE" -a "$VV" ] && echo -n "$LL$EE$VV "
47 [ "$ee" -a "$VV" ] && echo -n "$LL$ee$VV "
48 [ "$VV" ] && echo -n "$LL$VV "
49 [ "$CC" -a "$EE" ] && echo -n "$LL$CC$EE "
50 [ "$CC" -a "$ee" ] && echo -n "$LL$CC$ee "
51 [ "$CC" ] && echo -n "$LL$CC "
52 [ "$EE" ] && echo -n "$LL$EE "
53 [ "$ee" ] && echo -n "$LL$ee "
54 echo "$LL"
55 }
56 MY_LOCALES=$(locales_list $lang)
58 # Search and copy translation files
59 copy_translations() {
60 # for all packages in list
61 for P in $arg; do
63 echo "$((100*$NUM/$PKGNUM))" # send percentage to Yad client
64 NUM=$(($NUM+1)) # next package
66 # for all list types
67 for list_type in mo qm; do
68 IFS=$'\n'
69 for line in $(grep -e "^$P " $DATADIR/$PREFIX$list_type.list); do
70 locales=$(echo $line | cut -d' ' -f2)
71 names=$(echo $line | cut -d' ' -f3)
72 [ "x$names" == "x" ] && names=$P
73 paths=$(echo $line | cut -d' ' -f4)
74 [ "x$paths" == "x" ] && paths="$US/locale/%/$LC"
76 IFS=' '
77 # for all valid locale variants
78 for locale in $MY_LOCALES; do
79 if $(echo " $locales " | grep -q " $locale "); then
81 # for all file names
82 for name in $names; do
83 # for all paths
84 for path in $paths; do
85 # substitute variables and "%"
86 eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
88 # copy translation file to working dir
89 mkdir -p $WORKING$(dirname $fullname)
90 cp -pf $WOK/$P/install$fullname $WORKING$fullname
91 done
92 done
93 break
94 fi
95 done
96 done
97 done
98 done
99 }
101 #----------#
102 # Main #
103 #----------#
105 # Branch commands: list, rm, dl.
106 case "x$cmd" in
107 xlist) # Main actions: get list, search translations, make an archive.
108 # constants to use in lists
109 US="/usr/share"
110 LC="LC_MESSAGES"
111 PY="/usr/lib/python2.7/site-packages"
112 R="/usr/lib/R/library"
113 RT="$R/translations/%/$LC"
115 # Supported 4.0 (as stable now) and cooking (rolling, 5.0)
116 # Don't know what to do with "arm" and "x86_64" woks ???
117 case "x$rel" in
118 x4*|xstable) PREFIX="stable_"; WOK="stable" ;;
119 *) PREFIX=""; WOK="cooking" ;;
120 esac
121 # Path to the specified WOK in the SliTaz server.
122 WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
124 PKGNUM=$(echo $arg | wc -w) # number of packages in the list
125 NUM=1 # initial value
127 echo -e "Content-Type: text/plain\n\n" # to Yad client
128 echo "#Number of packages: $PKGNUM" # Message to Yad log
129 echo "#Searching in progress..." # And another one
131 copy_translations
133 echo "#" # Message to Yad log
134 echo "#Preparing archive. Please wait..." # And another one
136 # Make the archive from working dir and remove temp working dir.
137 busybox tar -czf $WORKING.tgz -C $WORKING .
138 rm -rf $WORKING
140 echo "#" # to Yad client log
141 echo "#Done!"
142 echo "#Now you can proceed to downloading"
143 echo "#and installing your translations."
144 echo "#File size: $(stat -c %s $WORKING.tgz) bytes."
146 echo "${WORKING#*.}" # give download token to Yad client
147 exit 0 ;;
149 xrm) # Remove archive.
150 # Avoid relative path to avoid removing of any system file.
151 archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
152 rm -f $archive
153 cat <<EOT
154 Content-Type: text/plain
155 Content-Length: 0
157 EOT
158 exit 0 ;;
160 xdl) # Send archive to client.
161 # Avoid relative path to avoid hijacking of any system file.
162 archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
163 cat <<EOT
164 Content-Type: application/x-compressed-tar
165 Content-Length: $(stat -c %s $archive)
166 Content-Disposition: attachment; filename=tramys.tgz
168 EOT
169 cat $archive
170 # Remove archive after sending.
171 rm -f $archive
172 exit 0 ;;
174 *) # Hide the script from the web bots and browsers.
175 echo -e "HTTP/1.0 404 Not Found\nContent-Type: text/html\n\n<!DOCTYPE html><html><head><title>404 - Not Found</title></head><body><h1>404 - Not Found</h1></body></html>"
176 exit ;;
177 esac
179 exit 0