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