wok annotate tramys-server/stuff/tramys2.cgi @ rev 17235

Up: firefox-official (33.0)
author Richard Dunbar <mojo@slitaz.org>
date Fri Oct 17 19:52:40 2014 -0400 (2014-10-17)
parents e96aa956e72e
children 02c70d036ea0
rev   line source
al@17081 1 #!/bin/sh
al@17081 2 # tramys - TRAnslate MY Slitaz. Server solution
al@17081 3 # Tool for managing translation files for SliTaz GNU/Linux
al@17081 4 # Aleksej Bobylev <al.bobylev@gmail.com>, 2014
al@17081 5
al@17082 6 # How to use:
al@17086 7 # 1. Request for archive:
al@17086 8 # HTTP_ACCEPT_LANGUAGE -> users locale
al@17086 9 # HTTP_ACCEPT -> SliTaz release
al@17086 10 # HTTP_COOKIE (list=...) -> space-separated list of packages to process
al@17086 11 #
al@17086 12 # 2. Remove archive that the user has refused to download:
al@17086 13 # HTTP_COOKIE (rm=DLKEY) -> remove /tmp/tmp.DLKEY.tgz file
al@17086 14 #
al@17086 15 # 3. Send archive to user:
al@17086 16 # HTTP_COOKIE (dl=DLKEY) -> send /tmp/tmp.DLKEY.tgz file
al@17081 17
al@17081 18 . /usr/bin/httpd_helper.sh
al@17092 19 . /home/slitaz/www/cook/tramys2.msg # translations
al@17081 20
al@17086 21 WORKING=$(busybox mktemp -d) # make temp working dir /tmp/tmp.??????
al@17086 22 DATADIR=/usr/share/tramys # this folder contains lists
al@17081 23
al@17086 24 # Get user settings from HTTP headers.
al@17086 25 lang="$HTTP_ACCEPT_LANGUAGE"
al@17086 26 rel="$HTTP_ACCEPT"
al@17086 27 cmd="${HTTP_COOKIE%%=*}"
al@17086 28 arg="${HTTP_COOKIE#*=}"
al@17082 29
al@17086 30 #-----------#
al@17086 31 # Functions #
al@17086 32 #-----------#
al@17082 33
al@17086 34 # Prepare list for search.
al@17086 35 # Original GNU gettext searches precisely in this order.
al@17081 36 locales_list() {
al@17081 37 LL=$(echo $1 | sed 's|^\([^_.@]*\).*$|\1|')
al@17081 38 CC=$(echo $1 | sed -n '/_/s|^[^_]*\(_[^.@]*\).*$|\1|p')
al@17081 39 EE=$(echo $1 | sed -n '/./s|^[^\.]*\(\.[^@]*\).*$|\1|p')
al@17081 40 VV=$(echo $1 | sed -n '/@/s|^[^@]*\(@.*\)$|\1|p')
al@17081 41 ee=$(echo $EE | tr A-Z a-z | tr -cd a-z0-9); [ "$ee" ] && ee=.$ee
al@17081 42 [ "x$EE" == "x$ee" ] && ee=''
al@17081 43
al@17081 44 [ "$CC" -a "$EE" -a "$VV" ] && echo -n "$LL$CC$EE$VV "
al@17081 45 [ "$CC" -a "$ee" -a "$VV" ] && echo -n "$LL$CC$ee$VV "
al@17081 46 [ "$CC" -a "$VV" ] && echo -n "$LL$CC$VV "
al@17081 47 [ "$EE" -a "$VV" ] && echo -n "$LL$EE$VV "
al@17081 48 [ "$ee" -a "$VV" ] && echo -n "$LL$ee$VV "
al@17081 49 [ "$VV" ] && echo -n "$LL$VV "
al@17081 50 [ "$CC" -a "$EE" ] && echo -n "$LL$CC$EE "
al@17081 51 [ "$CC" -a "$ee" ] && echo -n "$LL$CC$ee "
al@17081 52 [ "$CC" ] && echo -n "$LL$CC "
al@17081 53 [ "$EE" ] && echo -n "$LL$EE "
al@17081 54 [ "$ee" ] && echo -n "$LL$ee "
al@17081 55 echo "$LL"
al@17081 56 }
al@17086 57 MY_LOCALES=$(locales_list $lang)
al@17081 58
al@17086 59 # Search and copy translation files
al@17086 60 copy_translations() {
al@17086 61 # for all packages in list
al@17086 62 for P in $arg; do
al@17081 63
al@17086 64 echo "$((100*$NUM/$PKGNUM))" # send percentage to Yad client
al@17086 65 NUM=$(($NUM+1)) # next package
al@17082 66
al@17086 67 # for all list types
al@17086 68 for list_type in mo qm; do
al@17086 69 IFS=$'\n'
al@17086 70 for line in $(grep -e "^$P " $DATADIR/$PREFIX$list_type.list); do
al@17086 71 locales=$(echo $line | cut -d' ' -f2)
al@17086 72 names=$(echo $line | cut -d' ' -f3)
al@17086 73 [ "x$names" == "x" ] && names=$P
al@17086 74 paths=$(echo $line | cut -d' ' -f4)
al@17086 75 [ "x$paths" == "x" ] && paths="$US/locale/%/$LC"
al@17082 76
al@17086 77 IFS=' '
al@17086 78 # for all valid locale variants
al@17086 79 for locale in $MY_LOCALES; do
al@17086 80 if $(echo " $locales " | grep -q " $locale "); then
al@17082 81
al@17086 82 # for all file names
al@17086 83 for name in $names; do
al@17086 84 # for all paths
al@17086 85 for path in $paths; do
al@17086 86 # substitute variables and "%"
al@17086 87 eval "fullname=${path//%/$locale}/${name//%/$locale}.$list_type"
al@17081 88
al@17086 89 # copy translation file to working dir
al@17086 90 mkdir -p $WORKING$(dirname $fullname)
al@17086 91 cp -pf $WOK/$P/install$fullname $WORKING$fullname
al@17086 92 done
al@17081 93 done
al@17086 94 break
al@17086 95 fi
al@17086 96 done
al@17081 97 done
al@17081 98 done
al@17081 99 done
al@17086 100 }
al@17081 101
al@17086 102 #----------#
al@17086 103 # Main #
al@17086 104 #----------#
al@17082 105
al@17086 106 # Branch commands: list, rm, dl.
al@17086 107 case "x$cmd" in
al@17086 108 xlist) # Main actions: get list, search translations, make an archive.
al@17086 109 # constants to use in lists
al@17086 110 US="/usr/share"
al@17086 111 LC="LC_MESSAGES"
al@17086 112 PY="/usr/lib/python2.7/site-packages"
al@17086 113 R="/usr/lib/R/library"
al@17086 114 RT="$R/translations/%/$LC"
al@17081 115
al@17086 116 # Supported 4.0 (as stable now) and cooking (rolling, 5.0)
al@17086 117 # Don't know what to do with "arm" and "x86_64" woks ???
al@17086 118 case "x$rel" in
al@17086 119 x4*|xstable) PREFIX="stable_"; WOK="stable" ;;
al@17086 120 *) PREFIX=""; WOK="cooking" ;;
al@17086 121 esac
al@17086 122 # Path to the specified WOK in the SliTaz server.
al@17086 123 WOK="/home/slitaz/$WOK/chroot/home/slitaz/wok"
al@17081 124
al@17086 125 PKGNUM=$(echo $arg | wc -w) # number of packages in the list
al@17086 126 NUM=1 # initial value
al@17086 127
al@17086 128 echo -e "Content-Type: text/plain\n\n" # to Yad client
al@17092 129 msg 1 # Message to Yad log
al@17086 130
al@17086 131 copy_translations
al@17086 132
al@17092 133 msg 2 # Message to Yad log
al@17086 134
al@17086 135 # Make the archive from working dir and remove temp working dir.
al@17086 136 busybox tar -czf $WORKING.tgz -C $WORKING .
al@17086 137 rm -rf $WORKING
al@17086 138
al@17092 139 SIZE=$(ls -lh $WORKING.tgz | awk '{print $5}')
al@17092 140 msg 3 # Message to Yad log
al@17086 141
al@17086 142 echo "${WORKING#*.}" # give download token to Yad client
al@17086 143 exit 0 ;;
al@17086 144
al@17086 145 xrm) # Remove archive.
al@17086 146 # Avoid relative path to avoid removing of any system file.
al@17086 147 archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
al@17086 148 rm -f $archive
al@17086 149 cat <<EOT
al@17092 150 Content-Type: text/plain; charset=UTF-8
al@17086 151 Content-Length: 0
al@17086 152
al@17086 153 EOT
al@17086 154 exit 0 ;;
al@17086 155
al@17086 156 xdl) # Send archive to client.
al@17086 157 # Avoid relative path to avoid hijacking of any system file.
al@17086 158 archive="/tmp/tmp.$(echo $arg | tr -cd 'A-Za-z0-9').tgz"
al@17086 159 cat <<EOT
al@17086 160 Content-Type: application/x-compressed-tar
al@17086 161 Content-Length: $(stat -c %s $archive)
al@17086 162 Content-Disposition: attachment; filename=tramys.tgz
al@17086 163
al@17086 164 EOT
al@17086 165 cat $archive
al@17086 166 # Remove archive after sending.
al@17086 167 rm -f $archive
al@17086 168 exit 0 ;;
al@17086 169
al@17086 170 *) # Hide the script from the web bots and browsers.
al@17086 171 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>"
al@17086 172 exit ;;
al@17086 173 esac
al@17086 174
al@17082 175 exit 0