Guest User

TronUpdate V3.7

a guest
Jan 27th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.25 KB | None | 0 0
  1. #!/bin/bash
  2. #Automatically downloads new versions of tron script
  3. #This script is designed to update mirrors for tron script. You should probably run it as a cron job.
  4. #
  5. #
  6. #Tron script is located at http://reddit.com/r/tronscript
  7. #
  8. #Written by reddit.com/u/-jimmyrustles
  9. #VERSION 3.7
  10.  
  11. #CHANGELOG:
  12. #V1.0 Initial release
  13. #V1.1 Forgot double quotes around some variables.
  14. #V2.0 Added file integity check.
  15. #V3.0 Use HTTPS for updating instead of HTTP. Option to purge old versions when updating. Simplified and fixed several things. Added more comments and changed appearance.
  16. #V3.5 Added option to symlink updated file. Fixed "find" searching subdirectories when deleting.
  17. #V3.6 Do not use absolute symbolic links.
  18. #V3.7 Fix symlink getting deleted when updating
  19.  
  20. #USAGE:
  21. #./tronupdate.sh <directory to download tron in>
  22. #If directory is not specified, the script will default to the current directory
  23.  
  24.  
  25. #Change to "true" to purge all previous tron versions when updating.
  26. purgeoldversions="false"
  27.  
  28. #symlink latest tron version to "latest.exe" when updating
  29. #Change to "true" to enable
  30. symlink="false"
  31.  
  32.  
  33. #enable text colours
  34. red=$(tput setaf 1)
  35. green=$(tput setaf 2)
  36. blue=$(tput setaf 4)
  37. reset=$(tput sgr0)
  38. invert=$(tput rev)
  39.  
  40. # Set repo url
  41. repodir="https://jailhouse.sgc-hosting.com/~bmrforg/repos/tron" #You will most likely want to keep this as it is.
  42. #repodir="http://bmrf.org/repos/tron" #Fallback HTTP mirror
  43.  
  44. #repodir="http://localhost" #debug (you don't want this)
  45. #repodir="http://localhost/thisdoesnotexist" #debug (you don't want this)
  46.  
  47. # Set tron repo md5sums.txt file url
  48. md5sumsurl="https://jailhouse.sgc-hosting.com/~bmrforg/repos/tron/md5sums.txt" #You most likely want to keep this as it is.
  49. #md5sumsurl="http://bmrf.org/repos/tron/md5sums.txt" #Fallback HTTP mirror
  50.  
  51. #set download location (usually your tron mirror directory)
  52. #if directory is not specified on the command line, it will default to the current directory. This can be overwritten.
  53.  
  54. #change this to change the default directory
  55. defaultdownloaddir="."
  56.  
  57. if [ "$1" == "" ] #If directory not specified, use default
  58. then
  59.     echo "${red}Download directory not specified, using default (current directory)!${reset}"
  60.     echo
  61.     downloaddir=$defaultdownloaddir #use default directory
  62. else
  63.     downloaddir=$1 #use specified directory
  64. fi
  65.  
  66. #hash verify function
  67. function verify {
  68.     #Verify downloaded file using "md5sum"
  69.     echo "${green}VERIFYING DOWNLOADED FILE!${reset}"
  70.         #Get "correct" hash from md5sums.txt
  71.         correcthash=$(tail -n 1 "$downloaddir/md5sums.txt" | awk '{ print $1 }')
  72.         #Get hash of downloaded file
  73.         localhash=$(md5sum "$downloaddir/$updatefile" | awk '{ print $1 }')
  74.         #Print hashes
  75.         echo "${green}The hash should be:${red} $correcthash${reset}"
  76.         echo "${green}Your hash is:${red} $localhash${reset}"
  77.  
  78.         #Check if hashes match.
  79.         if [ "$correcthash" == "$localhash" ]
  80.         then
  81.                 verified="true"
  82.         else
  83.         verified="false"
  84.  
  85.         fi
  86.  
  87. }
  88.  
  89. #function that redownloads if not verified, quits if verified
  90. function checkifverified {
  91.         if [ "$verified" == "true" ]
  92.         then
  93.                 echo "${green}Hashes Match! $updatefile Verified!${reset}"
  94.                 echo "${green}MIRROR UPDATED!${reset}"
  95.         #Purge old versions (if enabled)
  96.         if [ "$purgeoldversions" == "true" ]
  97.         then
  98.                 echo "${red}Removing old tron versions!${reset}"
  99.                 echo "${blue}"
  100.                 #Produce list of old tron files, and use xargs to parse and delete these files.
  101.                 find "$downloaddir" -maxdepth 1 | grep 'Tron\|exe' | grep -v "$updatefile" | xargs -n 3 | sed 's/.*/"&"/' | xargs rm -f -v
  102.                 echo "${reset}"
  103.                 echo "${green}Done!${reset}"
  104.         fi
  105.        
  106.         #symlink updated tron file to "latest.exe" (if enabled)
  107.         if [ "$symlink" == "true" ]
  108.         then
  109.             echo "${blue}symlinking to \"latest.exe\"${reset}"
  110.             #change dir to download dir.
  111.             cd "$downloaddir"
  112.             #symlink. -s (symbolic) -f (force) -v (verbose)
  113.             ln -s -f -v "$updatefile" "latest.exe"
  114.             echo "${green}Done!${reset}"
  115.         fi
  116.         exit
  117.         else
  118.                 echo "${red}Hashes do not match! $updatefile does not match repo file! Will retry!${reset}"
  119.         #cleanup corrupt file
  120.         rm -f -v "$downloaddir/$updatefile"
  121.         #redownload
  122.         echo "${invert}"
  123.         wget -nc -O "$downloaddir/$updatefile" "$repodir/$updatefile"
  124.         echo "${reset}"
  125.         #increment "downloadtries" by 1
  126.         downloadtries=$((downloadtries + 1))
  127.         fi
  128. }
  129.  
  130.  
  131. #download md5sums.txt to $downloaddir if repo version is newer
  132. echo "${invert}"
  133. wget -P "$downloaddir" -N "$md5sumsurl"
  134. echo "${reset}"
  135.  
  136. #get latest version number
  137. latestversion=$(tail -n 1 "$downloaddir/md5sums.txt" | awk '{ print $3 }')
  138.  
  139. echo "${red}Latest version is:${green} $latestversion ${reset}"
  140.  
  141. #find latest local version
  142. localversion=$(ls -1 "$downloaddir" | grep 'Tron' | grep 'exe' | tail -n 1 | awk '{ print $2 }')
  143.  
  144. #if existing mirror does not exist, then fake older version
  145. if [ "$localversion" == "" ]
  146. then
  147.     echo "${red} EXISTING MIRROR NOT FOUND, WILL DOWNLOAD!${reset}"
  148.     localversion="v0.0.0"
  149. fi
  150. echo "${red}Your version:${green} $localversion ${reset}"
  151.  
  152. #compare local and remote versions
  153. if [ "$latestversion" == "$localversion" ]
  154. then
  155.     echo "MIRROR IS UP TO DATE!"
  156.     exit
  157. fi
  158.  
  159. if [ "$latestversion" != "$localversion" ]
  160. then
  161.     echo "MIRROR IS OUT OF DATE! UPDATING!"
  162.     #identify version to update to
  163.     updatefile=$(tail -n 1 "$downloaddir/md5sums.txt" | sed 's/*//g' | awk '{ print $2 " " $3 " " $4 }')
  164.     #actually download tron
  165.     echo "${invert}"
  166.     wget -nc -O "$downloaddir/$updatefile" "$repodir/$updatefile"
  167.     echo "${reset}"
  168.     #set download tries to "1"
  169.     downloadtries=1
  170.    
  171.     #Run while file is not verified
  172.     while [ "$verified" != "true" ]
  173.     do
  174.         #do while download attempts is less than 5
  175.         if (( downloadtries < 5 ))
  176.         then
  177.             verify
  178.             checkifverified
  179.         else
  180.             #fail if still can't verify after 5 downloads
  181.             echo "${red}After $downloadtries downloads $updatefile could still not be verified! Giving Up!${reset}"
  182.             #cleanup corrupt file
  183.             rm -f -v "$downloaddir/$updatefile"
  184.             #exit (obviously) :)
  185.             exit
  186.         fi
  187.  
  188.     done
  189. fi
  190.  
  191. #Oops.
  192. echo "${red}The script should never terminate by reaching the end! This should never be executed!${reset}"
Add Comment
Please, Sign In to add comment