Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.89 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. CWD=`dirname "$(readlink -f "$0")"`
  4.  
  5. if [ -e "$CWD/updater-cfg" ]; then
  6.     . "$CWD/updater-cfg"
  7.     firstLaunch=0
  8. else
  9.     firstLaunch=1
  10. fi
  11.  
  12. DOWNLOADSERVER=${DOWNLOADSERVER:--1}
  13. GAMEENGINE=${GAMEENGINE:--1}
  14. PASSWORD=${PASSWORD:-}
  15. CURRENTVERSION=${CURRENTVERSION:--1}
  16. ASKBEFOREUPDATING=${ASKBEFOREUPDATING:--1}
  17. UPDATERVERSION=${UPDATERVERSION:-4.0.3}
  18. APIURL="http://www.urbanterror.info/api/updaterv4/"
  19. URT_GAME_SUBDIR=${URT_GAME_SUBDIR:-q3ut4}
  20. BROWSER=${BROWSER:-}
  21. #PLATFORM=${PLATFORM:-Linux32}
  22.  
  23. if [ -z "$PLATFORM" ]; then
  24.     if [ "$(uname -m)" == "x86_64" ]; then
  25.         PLATFORM=Linux64
  26.     else
  27.         PLATFORM=Linux32
  28.     fi
  29. fi
  30.  
  31. XMLLINT=${XMLLINT:-`which xmllint`}
  32. CURL=${CURL:-`which curl`}
  33. if [ -z "$BROWSER" ]; then
  34.     if which lynx >/dev/null 2>&1 ; then
  35.         BROWSER=`which lynx`
  36.     elif which links >/dev/null 2>&1 ; then
  37.         BROWSER=`which links`
  38.     elif which elinks >/dev/null 2>&1 ; then
  39.         BROWSER=`which elinks`
  40.     fi
  41. fi
  42.  
  43. if [ ! -x "$XMLLINT" ]; then
  44.     echo "ERROR: xmllint not found. Install xmllint (part of libxml2, often split off as libxml2-utils or similar) or specify its location by passing XMLLINT=/path/to/xmllint to this script." 1>&2
  45.     exit 1
  46. fi
  47. if [ ! -x "$CURL" ]; then
  48.     echo "ERROR: curl not found. Install curl or specify its location by passing CURL=/path/to/curl to this script." 1>&2
  49.     exit 2
  50. fi
  51.  
  52. # Connect to the API and parse the result.
  53. # Pass the query as the first argument (all others are ignored),
  54. # eg. "versionInfo" or "versionFiles"
  55. function doManifest ()
  56. {
  57.     local manifest=$($CURL -X POST -H "Content-Type: application/x-www-form-urlencoded;charset=utf-8" -d "platform=${PLATFORM}" -d "query=$1" -d "password=${PASSWORD}" -d "version=${CURRENTVERSION}" -d "engine=${GAMEENGINE}" -d "server=${DOWNLOADSERVER}" -d "updaterVersion=${UPDATERVERSION}" $curlOpts "$APIURL")
  58.  
  59.     if [ -z "$manifest" ]; then
  60.         echo "ERROR: Failed to connect to API" 1>&2
  61.         exit 3
  62.     fi
  63.  
  64.     apiVersion=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/APIVersion/text())" - )
  65.     changelog=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Changelog/text())" - )
  66.     licenceText=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Licence/text())" - )
  67.  
  68.     newsList="<html><head><title>Urban Terror News</title></head><body style=\"background-color: black;\">"
  69.     for i in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/NewsList/NewsText)" - )`
  70.     do
  71.         newsList="${newsList}<br />
  72. $(echo "$manifest" | $XMLLINT --xpath "string(/Updater/NewsList/NewsText[$i]/text())" - )"
  73.    done
  74.    newsList="${newsList}</body></html>"
  75.  
  76.    serverCount=0
  77.    unset serverName
  78.    unset serverURL
  79.    unset serverLocation
  80.    unset serverId
  81.    for i in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/ServerList/Server)" - )`
  82.    do
  83.        serverCount=$((${serverCount}+1))
  84.        serverName[$serverCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/ServerList/Server[$i]/ServerName/text())" - )
  85.         serverURL[$serverCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/ServerList/Server[$i]/ServerURL/text())" - )
  86.         serverLocation[$serverCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/ServerList/Server[$i]/ServerLocation/text())" - )
  87.         serverId[$serverCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/ServerList/Server[$i]/ServerId/text())" - )
  88.     done
  89.  
  90.     engineCount=0
  91.     unset engineName
  92.     unset engineDir
  93.     unset engineId
  94.     unset engineLaunchString
  95.     for i in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/EngineList/Engine)" - )`
  96.     do
  97.         engineCount=$((${engineCount}+1))
  98.         engineName[$engineCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/EngineList/Engine[$i]/EngineName/text())" - )
  99.         engineDir[$engineCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/EngineList/Engine[$i]/EngineDir/text())" - )
  100.         engineId[$engineCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/EngineList/Engine[$i]/EngineId/text())" - )
  101.         engineLaunchString[$engineCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/EngineList/Engine[$i]/EngineLaunchString/text())" - )
  102.     done
  103.  
  104.     versionCount=0
  105.     unset versionName
  106.     unset versionId
  107.     for i in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/VersionList/Version)" - )`
  108.     do
  109.         versionCount=$((${versionCount}+1))
  110.         versionName[$versionCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/VersionList/Version[$i]/VersionName/text())" - )
  111.         versionId[$versionCount]=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/VersionList/Version[$i]/VersionNumber/text())" - )
  112.     done
  113.  
  114.     fileCount=0
  115.     local mustDownload
  116.     unset filePath
  117.     unset fileName
  118.     unset fileMd5
  119.     unset fileSize
  120.     unset fileUrl
  121.     unset packsList
  122.     # There can be multiple Files sections (one for zUrT_*.pk3 and one for
  123.     # the engine) so use nested loops
  124.     for i in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/Files)" - )`
  125.     do
  126.         for j in `seq $(echo "$manifest" | $XMLLINT --xpath "count(/Updater/Files[$i]/File)" - )`
  127.         do
  128.             tmpFileDir=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Files[$i]/File[$j]/FileDir/text())" - )
  129.             tmpFileName=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Files[$i]/File[$j]/FileName/text())" - )
  130.             tmpFileMD5=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Files[$i]/File[$j]/FileMD5/text())" - )
  131.             tmpFileSize=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Files[$i]/File[$j]/FileSize/text())" - )
  132.             tmpFileUrl=$(echo "$manifest" | $XMLLINT --xpath "string(/Updater/Files[$i]/File[$j]/FileUrl[last()]/text())" - )
  133.  
  134.             mustDownload=0
  135.  
  136.             if [ -n "$tmpFileName" ]; then
  137.                 # If file does not exist, download
  138.                 if ! [ -e "${CWD}/${tmpFileDir}/${tmpFileName}" ]; then
  139.                     mustDownload=1
  140.                 # If wrong MD5, download
  141.                 elif ! echo "${tmpFileMD5}  ${CWD}/${tmpFileDir}/${tmpFileName}" | md5sum -c >/dev/null 2>&1 ; then
  142.                     mustDownload=1
  143.                 fi
  144.                 # Empty MD5 = delete this file
  145.                 if [ -z "$tmpFileMD5" ]; then
  146.                     rm -f "${CWD}/${tmpFileDir}/${tmpFileName}"
  147.                     mustDownload=0
  148.                 fi
  149.                 packsList[$((${#packsList[*]}+1))]="$tmpFileName"
  150.             fi
  151.  
  152.             if [ $mustDownload -eq 1 ]; then
  153.                 fileCount=$((${fileCount}+1))
  154.                 filePath[$fileCount]="$tmpFileDir"
  155.                 fileName[$fileCount]="$tmpFileName"
  156.                 fileMd5[$fileCount]="$tmpFileMD5"
  157.                 fileSize[$fileCount]="$tmpFileSize"
  158.                 fileUrl[$fileCount]="$tmpFileUrl"
  159.             fi
  160.         done
  161.     done
  162. }
  163.  
  164. function checkAPIVersion ()
  165. {
  166.     if [ "$apiVersion" != "$UPDATERVERSION" ]; then
  167.         echo "ERROR: This version (${UPDATERVERSION}) of the Updater is outdated. Please download the new Updater here: http://get.urbanterror.info" 1>&2
  168.         exit 4
  169.     fi
  170. }
  171.  
  172. function apiError ()
  173. {
  174.     echo "ERROR: Information from the API is missing or wrong. Please report it on www.urbanterror.info and try again later." 1>&2
  175.     exit 5
  176. }
  177.  
  178. function checkDownloadServer ()
  179. {
  180.     if [ $serverCount -lt 1 ]; then
  181.         apiError
  182.     fi
  183.  
  184.     local found=0
  185.     for i in `seq $serverCount`
  186.     do
  187.         if [ -n "${serverId[$i]}" -a "$DOWNLOADSERVER" == "${serverId[$i]}" ]; then
  188.             found=1
  189.             break
  190.         fi
  191.     done
  192.  
  193.     if [ $found -ne 1 ]; then
  194.         DOWNLOADSERVER=${serverId[1]}
  195.     fi
  196. }
  197.  
  198. function checkGameEngine ()
  199. {
  200.     if [ $engineCount -lt 1 ]; then
  201.         apiError
  202.     fi
  203.  
  204.     local found=0
  205.     for i in `seq $engineCount`
  206.     do
  207.         if [ -n "${engineId[$i]}" -a "$GAMEENGINE" == "${engineId[$i]}" ]; then
  208.             found=1
  209.             break
  210.         fi
  211.     done
  212.  
  213.     if [ $found -ne 1 ]; then
  214.         GAMEENGINE=${engineId[1]}
  215.     fi
  216. }
  217.  
  218. function checkVersion ()
  219. {
  220.     if [ $versionCount -lt 1 ]; then
  221.         apiError
  222.     fi
  223.  
  224.     local found=0
  225.     for i in `seq $versionCount`
  226.     do
  227.         if [ -n "${versionId[$i]}" -a "$CURRENTVERSION" == "${versionId[$i]}" ]; then
  228.             found=1
  229.             break
  230.         fi
  231.     done
  232.  
  233.     if [ $found -ne 1 ]; then
  234.         CURRENTVERSION=${versionId[1]}
  235.     fi
  236. }
  237.  
  238. function doBrowser ()
  239. {
  240.     TMPFILEBIN=`which tempfile 2> /dev/null`
  241.     if [ -z "$TMPFILEBIN" ]; then
  242.         TMPFILE="tmp`date +%y%m%H%M%S`.html"
  243.     else
  244.         TMPFILE=`$TMPFILEBIN -d "$CWD" -p tmp -s .html`
  245.     fi
  246.  
  247.     echo "$1" > "$TMPFILE"
  248.  
  249.     if [ -z "$BROWSER" ]; then
  250.         cat "$TMPFILE"
  251.     else
  252.         $BROWSER "$TMPFILE"
  253.     fi
  254.  
  255.     rm -f "$TMPFILE"
  256. }
  257.  
  258. function drawLicence ()
  259. {
  260.     if [ $runQuiet -eq 1 ]; then
  261.         return
  262.     fi
  263.  
  264.     if [ -z "$licenceText" ]; then
  265.         apiError
  266.     fi
  267.  
  268.     doBrowser "$licenceText"
  269. }
  270.  
  271. function drawNews ()
  272. {
  273.     if [ $runQuiet -eq 1 ]; then
  274.         return
  275.     fi
  276.  
  277.     if [ -z "$newsList" ]; then
  278.         apiError
  279.     fi
  280.  
  281.     doBrowser "$newsList"
  282. }
  283.  
  284. function checkFiles ()
  285. {
  286.     local found
  287.     if [ ${#packsList[*]} -gt 0 ]; then
  288.         if [ -d "${CWD}/${URT_GAME_SUBDIR}" ]; then
  289.             cd "${CWD}/${URT_GAME_SUBDIR}"
  290.             for i in zUrT*.pk3
  291.             do
  292.                 if [ "$i" == "zUrT*.pk3" ]; then
  293.                     break
  294.                 fi
  295.                 found=0
  296.                 for j in `seq ${#packsList[*]}`
  297.                 do
  298.                     if [ "${packsList[$j]}" == "$i" ]; then
  299.                         found=1
  300.                         break
  301.                     fi
  302.                 done
  303.                 if [ $found -eq 0 ]; then
  304.                     rm -f "$i"
  305.                 fi
  306.             done
  307.         fi
  308.     fi
  309.     cd "${CWD}"
  310. }
  311.  
  312. function playGame ()
  313. {
  314.     if [ $runQuiet -eq 1 ]; then
  315.         return
  316.     fi
  317.  
  318.     read -p "Would you like to play now? [y/n]: " -e INPUT
  319.     if [ "$INPUT" == "y" -o "$INPUT" == "Y" ]; then
  320.         for i in `seq $engineCount`
  321.         do
  322.             if [ -n "${engineId[$i]}" -a "$GAMEENGINE" == "${engineId[$i]}" ]; then
  323.                 "${CWD}/${engineDir[$i]}/${engineLaunchString[$i]}"
  324.                 break
  325.             fi
  326.         done
  327.     fi
  328. }
  329.  
  330. function downloadFiles ()
  331. {
  332.     local errored=0
  333.     if [ $fileCount -eq 0 ]; then
  334.         if [ $runQuiet -eq 0 ]; then
  335.             echo 'Your game is up to date!'
  336.         fi
  337.         playGame
  338.     else
  339.         if [ $ASKBEFOREUPDATING -eq 1 -a $runQuiet -eq 0 ]; then
  340.             read -p "A new update is available. Would you like to download it now? [y/n]: " -e INPUT
  341.             if [ "$INPUT" != "y" -a "$INPUT" != "Y" ]; then
  342.                 echo 'Update cancelled - your game is outdated!'
  343.                 playGame
  344.                 return
  345.             fi
  346.         fi
  347.         for i in `seq $fileCount`
  348.         do
  349.             cd "${CWD}/${filePath[$i]}" || exit 6
  350.             if [ -n "${fileUrl[$i]}" -a -n "${fileName[$i]}" ]; then
  351.                 $CURL $curlOpts "${fileUrl[$i]}" -o "${fileName[$i]}"
  352.                 if ! echo "${fileMd5[$i]}  ${fileName[$i]}" | md5sum -c >/dev/null 2>&1 ; then
  353.                     echo "ERROR: Downloaded file (${fileName[$i]}) is corrupt\! Re-run this updater to try again..." 1>&2
  354.                     errored=1
  355.                     rm -f "${fileName[$i]}"
  356.                 fi
  357.                 if [[ "${fileName[$i]}" =~ .*\.i386.* ]] || [[ "${fileName[$i]}" =~ .*\.x86_64.* ]]; then
  358.                     chmod +x "${fileName[$i]}"
  359.                 fi
  360.             else
  361.                 apiError
  362.             fi
  363.         done
  364.         cd "${CWD}"
  365.         if [ $errored -eq 1 ]; then
  366.             echo "ERROR: Some downloaded files were corrupt. Re-run the updater to try again." 1>&2
  367.             exit 7
  368.         fi
  369.         if [ $runQuiet -eq 0 ]; then
  370.             echo 'Your game is now up to date!'
  371.         fi
  372.         playGame
  373.     fi
  374. }
  375.  
  376. function writeSettings() {
  377.     cat > "$CWD/updater-cfg" <<EOF
  378. # UrT server updater configuration
  379. CURRENTVERSION=\${CURRENTVERSION:-${CURRENTVERSION}}
  380. DOWNLOADSERVER=\${DOWNLOADSERVER:-${DOWNLOADSERVER}}
  381. GAMEENGINE=\${GAMEENGINE:-${GAMEENGINE}}
  382. ASKBEFOREUPDATING=\${ASKBEFOREUPDATING:-${ASKBEFOREUPDATING}}
  383. EOF
  384. }
  385.  
  386. function openSettings () {
  387.     echo
  388.     echo "Choose a version:"
  389.     echo -e "ID\tVersion"
  390.     for i in `seq $versionCount`
  391.     do
  392.         echo -e "${i}\t${versionName[$i]}"
  393.     done
  394.     read -p "Enter a version ID (or anything else to cancel): " -e INPUT
  395.     if [ -n "$INPUT" -a -n "${versionId[$INPUT]}" ]; then
  396.         CURRENTVERSION=${versionId[$INPUT]}
  397.     fi
  398.     echo
  399.     echo "Choose a download mirror:"
  400.     echo -e "ID\tLocation\tName"
  401.     for i in `seq $serverCount`
  402.     do
  403.         echo -e "${i}\t${serverLocation[$i]}\t${serverName[$i]}"
  404.     done
  405.     read -p "Enter a server ID (or anything else to cancel): " -e INPUT
  406.     if [ -n "$INPUT" -a -n "${serverId[$INPUT]}" ]; then
  407.         DOWNLOADSERVER=${serverId[$INPUT]}
  408.     fi
  409.     echo
  410.     echo "Choose a game engine:"
  411.     echo -e "ID\tEngine"
  412.     for i in `seq $engineCount`
  413.     do
  414.         echo -e "${i}\t${engineName[$i]}"
  415.     done
  416.     read -p "Enter an engine ID (or anything else to cancel): " -e INPUT
  417.     if [ -n "$INPUT" -a -n "${engineId[$INPUT]}" ]; then
  418.         GAMEENGINE=${engineId[$INPUT]}
  419.     fi
  420.     echo
  421.     read -p "Update automatically without asking? [y/n]: " -e INPUT
  422.     if [ "$INPUT" == "n" -o "$INPUT" == "N" ]; then
  423.         ASKBEFOREUPDATING=1
  424.     else
  425.         ASKBEFOREUPDATING=0
  426.     fi
  427.     writeSettings
  428. }
  429.  
  430. if [ "$1" == "-h" -o "$1" == "--help" ]; then
  431.     cat <<EOF
  432. Usage: $0 [OPTIONS]
  433. Update Urban Terror
  434.  
  435.   -h, --help          This help
  436.   -s, --settings      Modify updater settings
  437.   -q, --quiet         Run non-interactively (for cron use etc.)
  438. EOF
  439.     exit
  440. fi
  441.  
  442. runQuiet=0
  443. curlOpts=""
  444. if [ "$1" == "-q" -o "$1" == "--quiet" ]; then
  445.     runQuiet=1
  446.     curlOpts="-s"
  447. fi
  448.  
  449. doManifest "versionInfo"
  450. checkAPIVersion
  451. checkDownloadServer
  452. checkGameEngine
  453. checkVersion
  454. drawNews
  455.  
  456. if [ $firstLaunch -eq 1 ]; then
  457.     if [ $runQuiet -eq 1 ]; then
  458.         echo "ERROR: Initial run must be interactive."
  459.         exit 9
  460.     fi
  461.     drawLicence
  462.     read -p "Do you accept the terms of this licence? [y/n]: " -e INPUT
  463.     if [ "$INPUT" != "y" -a "$INPUT" != "Y" ]; then
  464.         echo "Licence terms rejected. Exiting." 1>&2
  465.         exit 8
  466.     fi
  467.  
  468.     mkdir "${CWD}/${URT_GAME_SUBDIR}"
  469.     openSettings
  470. fi
  471. if [ ! -d "${CWD}/${URT_GAME_SUBDIR}" ]; then
  472.     mkdir "${CWD}/${URT_GAME_SUBDIR}"
  473. fi
  474. if [ "$1" == "-s" -o "$1" == "--settings" ]; then
  475.     openSettings
  476. fi
  477.  
  478. doManifest "versionFiles"
  479. checkAPIVersion
  480. checkDownloadServer
  481. checkGameEngine
  482. checkVersion
  483. checkFiles
  484. downloadFiles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement