Page 1 of 1

script to keep latest+stable dedicated servers updated on linux

Posted: Sat May 20, 2023 9:47 pm
by quyxkh

Code: Select all

#!/bin/bash
# put this in your /opt/factorio or wherever you want your two server installs kept,
# it'll check releases, do any fetching and set the `latest` and `stable` symlinks
while getopts vx-: opt; do case $opt in   # just debugging options here
	x) set -x ;; v) set -v;;
	*) echo $opt $OPTIND $OPTARG ;;
esac; done; shift $((_=--OPTIND,OPTIND=1,_))

set -a
declare -A url fn v size etag latest stable
source /dev/stdin <<EOD
$(
export LC_ALL=C
for version in stable latest; do (
	checkurl=https://factorio.com/get-download/$version/headless/linux64
	# scrape the HTTP bounce and download metadata for useful values
	curl -sLI $checkurl | sed -nE '
		s,[[:space:]]+$,,
		/^HTTP[^ ]* 302/,/^$/ {
			/^location:/ {		s,.* ,url['$version']=,p;
				s,\?.*,,;	s,.*/,fn['$version']=,p
				s,\.tar\..*,,;	s,.*_,v['$version']=,p
			}
		}
		/^HTTP[^ ]* 200/,/^$/ {
			/^content-length:/	s,.* ,size['$version']=,p;
			/^etag:/		s,.* ,etag['$version']=,p;
		}
	'
) & done
wait
)
EOD

fetch() {
	if [[ ! -r ${fn[$1]} || `stat -c%s ${fn[$1]}` != ${size[$1]} ]]
	then curl -C - -o ${fn[$1]} ${url[$1]} &
	fi
}
fetch stable; if [[ ${v[latest]} != ${v[stable]} ]]; then fetch latest; fi
wait

for version in stable latest
do (	url=${url[$version]} fn=${fn[$version]} v=${v[$version]} size=${size[$version]}
	mkdir -p $v
	ln -sfT $v/factorio $version
	if [[ `stat -c%y $v` != `stat -c%y $fn` ]]
       	then	tar Cxf $v $fn || exit
		touch -r $fn $v
       	fi
) &
done
wait