Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Automatically downloads new versions of tron script
- #This script is designed to update mirrors for tron script. You should probably run it as a cron job.
- #
- #
- #Tron script is located at http://reddit.com/r/tronscript
- #
- #Written by reddit.com/u/-jimmyrustles
- #VERSION 3.7
- #CHANGELOG:
- #V1.0 Initial release
- #V1.1 Forgot double quotes around some variables.
- #V2.0 Added file integity check.
- #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.
- #V3.5 Added option to symlink updated file. Fixed "find" searching subdirectories when deleting.
- #V3.6 Do not use absolute symbolic links.
- #V3.7 Fix symlink getting deleted when updating
- #USAGE:
- #./tronupdate.sh <directory to download tron in>
- #If directory is not specified, the script will default to the current directory
- #Change to "true" to purge all previous tron versions when updating.
- purgeoldversions="false"
- #symlink latest tron version to "latest.exe" when updating
- #Change to "true" to enable
- symlink="false"
- #enable text colours
- red=$(tput setaf 1)
- green=$(tput setaf 2)
- blue=$(tput setaf 4)
- reset=$(tput sgr0)
- invert=$(tput rev)
- # Set repo url
- repodir="https://jailhouse.sgc-hosting.com/~bmrforg/repos/tron" #You will most likely want to keep this as it is.
- #repodir="http://bmrf.org/repos/tron" #Fallback HTTP mirror
- #repodir="http://localhost" #debug (you don't want this)
- #repodir="http://localhost/thisdoesnotexist" #debug (you don't want this)
- # Set tron repo md5sums.txt file url
- md5sumsurl="https://jailhouse.sgc-hosting.com/~bmrforg/repos/tron/md5sums.txt" #You most likely want to keep this as it is.
- #md5sumsurl="http://bmrf.org/repos/tron/md5sums.txt" #Fallback HTTP mirror
- #set download location (usually your tron mirror directory)
- #if directory is not specified on the command line, it will default to the current directory. This can be overwritten.
- #change this to change the default directory
- defaultdownloaddir="."
- if [ "$1" == "" ] #If directory not specified, use default
- then
- echo "${red}Download directory not specified, using default (current directory)!${reset}"
- echo
- downloaddir=$defaultdownloaddir #use default directory
- else
- downloaddir=$1 #use specified directory
- fi
- #hash verify function
- function verify {
- #Verify downloaded file using "md5sum"
- echo "${green}VERIFYING DOWNLOADED FILE!${reset}"
- #Get "correct" hash from md5sums.txt
- correcthash=$(tail -n 1 "$downloaddir/md5sums.txt" | awk '{ print $1 }')
- #Get hash of downloaded file
- localhash=$(md5sum "$downloaddir/$updatefile" | awk '{ print $1 }')
- #Print hashes
- echo "${green}The hash should be:${red} $correcthash${reset}"
- echo "${green}Your hash is:${red} $localhash${reset}"
- #Check if hashes match.
- if [ "$correcthash" == "$localhash" ]
- then
- verified="true"
- else
- verified="false"
- fi
- }
- #function that redownloads if not verified, quits if verified
- function checkifverified {
- if [ "$verified" == "true" ]
- then
- echo "${green}Hashes Match! $updatefile Verified!${reset}"
- echo "${green}MIRROR UPDATED!${reset}"
- #Purge old versions (if enabled)
- if [ "$purgeoldversions" == "true" ]
- then
- echo "${red}Removing old tron versions!${reset}"
- echo "${blue}"
- #Produce list of old tron files, and use xargs to parse and delete these files.
- find "$downloaddir" -maxdepth 1 | grep 'Tron\|exe' | grep -v "$updatefile" | xargs -n 3 | sed 's/.*/"&"/' | xargs rm -f -v
- echo "${reset}"
- echo "${green}Done!${reset}"
- fi
- #symlink updated tron file to "latest.exe" (if enabled)
- if [ "$symlink" == "true" ]
- then
- echo "${blue}symlinking to \"latest.exe\"${reset}"
- #change dir to download dir.
- cd "$downloaddir"
- #symlink. -s (symbolic) -f (force) -v (verbose)
- ln -s -f -v "$updatefile" "latest.exe"
- echo "${green}Done!${reset}"
- fi
- exit
- else
- echo "${red}Hashes do not match! $updatefile does not match repo file! Will retry!${reset}"
- #cleanup corrupt file
- rm -f -v "$downloaddir/$updatefile"
- #redownload
- echo "${invert}"
- wget -nc -O "$downloaddir/$updatefile" "$repodir/$updatefile"
- echo "${reset}"
- #increment "downloadtries" by 1
- downloadtries=$((downloadtries + 1))
- fi
- }
- #download md5sums.txt to $downloaddir if repo version is newer
- echo "${invert}"
- wget -P "$downloaddir" -N "$md5sumsurl"
- echo "${reset}"
- #get latest version number
- latestversion=$(tail -n 1 "$downloaddir/md5sums.txt" | awk '{ print $3 }')
- echo "${red}Latest version is:${green} $latestversion ${reset}"
- #find latest local version
- localversion=$(ls -1 "$downloaddir" | grep 'Tron' | grep 'exe' | tail -n 1 | awk '{ print $2 }')
- #if existing mirror does not exist, then fake older version
- if [ "$localversion" == "" ]
- then
- echo "${red} EXISTING MIRROR NOT FOUND, WILL DOWNLOAD!${reset}"
- localversion="v0.0.0"
- fi
- echo "${red}Your version:${green} $localversion ${reset}"
- #compare local and remote versions
- if [ "$latestversion" == "$localversion" ]
- then
- echo "MIRROR IS UP TO DATE!"
- exit
- fi
- if [ "$latestversion" != "$localversion" ]
- then
- echo "MIRROR IS OUT OF DATE! UPDATING!"
- #identify version to update to
- updatefile=$(tail -n 1 "$downloaddir/md5sums.txt" | sed 's/*//g' | awk '{ print $2 " " $3 " " $4 }')
- #actually download tron
- echo "${invert}"
- wget -nc -O "$downloaddir/$updatefile" "$repodir/$updatefile"
- echo "${reset}"
- #set download tries to "1"
- downloadtries=1
- #Run while file is not verified
- while [ "$verified" != "true" ]
- do
- #do while download attempts is less than 5
- if (( downloadtries < 5 ))
- then
- verify
- checkifverified
- else
- #fail if still can't verify after 5 downloads
- echo "${red}After $downloadtries downloads $updatefile could still not be verified! Giving Up!${reset}"
- #cleanup corrupt file
- rm -f -v "$downloaddir/$updatefile"
- #exit (obviously) :)
- exit
- fi
- done
- fi
- #Oops.
- 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