Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # /etc/init.d/vintagestory.sh
- # version 0.4.2 2016-02-09 (YYYY-MM-DD)
- #
- ### BEGIN INIT INFO
- # Provides: vintagestory
- # Required-Start: $local_fs $remote_fs screen-cleanup
- # Required-Stop: $local_fs $remote_fs
- # Should-Start: $network
- # Should-Stop: $network
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: vintagestory server
- # Description: Starts the vintagestory server
- ### END INIT INFO
- # Shamelessly adapted from http://minecraft.gamepedia.com/Tutorials/Server_startup_script
- #Settings
- SERVICE='VintagestoryServer.exe'
- SCREENNAME='vintagestory_server'
- OPTIONS=''
- USERNAME='vintagestory'
- WORLD='world'
- HISTORY=1024
- VSPATH='/home/vintagestory/server'
- INVOCATION="mono $SERVICE $OPTIONS"
- #Commands
- command -v mono >/dev/null 2>&1 || { echo "I require mono but it's not installed. Aborting." >&2; exit 1; }
- command -v monodis >/dev/null 2>&1 || { echo "I require monodis but it's not installed. Aborting." >&2; exit 1; }
- command -v curl >/dev/null 2>&1 || { echo "I require curl but it's not installed. Aborting." >&2; exit 1; }
- command -v wget >/dev/null 2>&1 || { echo "I require wget but it's not installed. Aborting." >&2; exit 1; }
- command -v screen >/dev/null 2>&1 || { echo "I require screen but it's not installed. Aborting." >&2; exit 1; }
- ME=`whoami`
- as_user() {
- if [ "$ME" = "$USERNAME" ] ; then
- bash -c "$1"
- else
- su - "$USERNAME" -c "$1"
- fi
- }
- vs_start() {
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- echo "$SERVICE is already running!"
- else
- echo "Starting $SERVICE..."
- cd $VSPATH
- as_user "cd $VSPATH && screen -h $HISTORY -dmS ${SCREENNAME} $INVOCATION"
- sleep 7
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- echo "$SERVICE is now running."
- else
- echo "Error! Could not start $SERVICE!"
- fi
- fi
- }
- vs_stop() {
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- echo "Stopping $SERVICE"
- as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
- sleep 10
- as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"/stop\"\015'"
- sleep 7
- else
- echo "$SERVICE was not running."
- fi
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- echo "Error! $SERVICE could not be stopped."
- else
- echo "$SERVICE is stopped."
- fi
- }
- vs_command() {
- command="$1";
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- pre_log_len=`wc -l "$VSPATH/VintagestoryData/Logs/server-main.txt" | awk '{print $1}'`
- echo "$SERVICE is running... executing command"
- as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"/$command\"\015'"
- sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
- # print output
- tail -n $[`wc -l "$VSPATH/VintagestoryData/Logs/server-main.txt" | awk '{print $1}'`-$pre_log_len] "$VSPATH/VintagestoryData/Logs/server-main.txt"
- fi
- }
- vercomp () {
- if [[ $1 == $2 ]]
- then
- return 0
- fi
- local IFS=.
- local i ver1=($1) ver2=($2)
- # fill empty fields in ver1 with zeros
- for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
- do
- ver1[i]=0
- done
- for ((i=0; i<${#ver1[@]}; i++))
- do
- if [[ -z ${ver2[i]} ]]
- then
- # fill empty fields in ver2 with zeros
- ver2[i]=0
- fi
- if ((10#${ver1[i]} > 10#${ver2[i]}))
- then
- return 1
- fi
- if ((10#${ver1[i]} < 10#${ver2[i]}))
- then
- return 2
- fi
- done
- return 0
- }
- testvercomp () {
- vercomp $1 $2
- case $? in
- 0) op='=';;
- 1) op='>';;
- 2) op='<';;
- esac
- if [[ $op == '=' ]]
- then
- echo "Your Vintage Story installation is already newest version $2"
- exit
- else
- echo "Update"
- fi
- }
- vs_update () {
- cd $VSPATH
- oldversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
- newversion=$(wget http://version.vintagestory.at/version.txt -q -O -)
- testvercomp $oldversion $newversion '<'
- vs_install $newversion
- installedversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
- echo Finished update to $installedversion
- vs_start
- }
- vs_reinstall () {
- newversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
- vs_install $newversion
- echo Finished reinstall to $newversion
- vs_start
- }
- vs_install () {
- cd $VSPATH
- newversion=$(echo $1 | cut -c-5)
- echo Start downloading
- curl http://account.vintagestory.at/files/vs_server_$newversion.tar.gz -o vs_server_$newversion.tar.gz -L
- tar -xzf vs_server_$newversion.tar.gz
- vs_command "/announce Short restart for a Vintage Story update"
- vs_stop
- vs_backup
- rm -rf vs_server_$newversion.tar.gz
- sed -i "s/USERNAME='vintagestory'/USERNAME='$USERNAME'/" server.sh
- SEDVSPATH="$(echo $VSPATH | sed 's/\//\\\//g')"
- sed -i "s/VSPATH='\/home\/vintagestory\/server'/VSPATH='$SEDVSPATH'/" server.sh
- chmod 755 server.sh
- }
- vs_backup () {
- cd $VSPATH
- version=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c16-)
- if [ ! -d "backup" ]; then
- mkdir "backup"
- fi
- echo Start backup
- aktualtime=$(date +%d.%m.%y-%R)
- tar czf backup/vs-server-$aktualtime-v$version.tar.gz * --exclude=backup
- echo Finished backup to vs-archiv-$aktualtime-v$version.tar.gz
- }
- case "$1" in
- start)
- vs_start
- ;;
- stop)
- vs_stop
- ;;
- restart)
- vs_stop
- vs_start
- ;;
- update)
- vs_update
- ;;
- backup)
- vs_backup
- ;;
- reinstall)
- vs_reinstall
- ;;
- status)
- if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
- echo "$SERVICE is running."
- else
- echo "$SERVICE is not running."
- fi
- ;;
- command)
- if [ $# -gt 1 ] ; then
- shift
- vs_command "$*"
- else
- echo "Must specify server command (try 'help'?)"
- fi
- ;;
- *)
- echo "Usage: $0 {start|stop|status|restart|update|backup|reinstall|command \"server command\"}"
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement