Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #############################################################################
- # MINECRAFT SERVER LAUNCHER
- ###########################
- #
- # This is a rather basic server launcher that gets the HTTP Headers for the
- # minecraft server jar file and compares the Last-Modified header with a
- # local copy of the header. If these values are different the script
- # downloads a new version of the server jar. It then updates the local
- # copy of the header value.
- #
- #############################################################################
- # HELP:
- ###########################
- #
- # ./launcher.bash
- # Runs the version check only
- #
- # ./laucnher.bash start
- # Runs the version check and then starts the server with the defaults
- #
- # ./launcher.bash start="parms"
- # Runs the version check and then starts the server with the 'params'
- #
- # ./launcher.bash path="path"
- # Starts by changing the directory to 'path' and runs the version check
- #
- # ./launcher.bash archive
- # If the checksums are difference, archive the world before existing
- #
- #############################################################################
- # LICENCE
- ###########################
- #
- # Copyright 2011 Nicholas Steicke. All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without modification, are
- # permitted provided that the following conditions are met:
- #
- # 1. Redistributions of source code must retain the above copyright notice, this list of
- # conditions and the following disclaimer.
- #
- # 2. Redistributions in binary form must reproduce the above copyright notice, this list
- # of conditions and the following disclaimer in the documentation and/or other materials
- # provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY NICHOLAS STEICKE ''AS IS'' AND ANY EXPRESS OR IMPLIED
- # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NICHOLAS STEICKE OR
- # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #
- # The views and conclusions contained in the software and documentation are those of the
- # authors and should not be interpreted as representing official policies, either expressed
- # or implied, of Nicholas Steicke.
- which="/usr/bin/which"
- rm=`$which rm`
- ln=`$which ln`
- touch=`$which touch`
- cat=`$which cat`
- curl=`$which curl`
- grep=`$which grep`
- sed=`$which sed`
- java=`$which java`
- sha1sum=`$which sha1sum`
- tar=`$which tar`
- mkdir=`which mkdir`
- python=`$which python`
- SERVER_URL="https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"
- WDIR="."
- START=false
- START_PARAMS="-Xmx1024M -Xms1024M"
- ARCHIVE=false
- while [ $# -ne 0 ]; do
- if [[ ${1:0:5} == "path=" ]]; then
- WDIR=${1:5}
- elif [[ ${1:0:5} == "start" ]]; then
- START=true
- if [[ ${1:5} != "" ]]; then
- START_PARAMS=${1:5}
- fi
- elif [[ $1 == "archive" ]]; then
- ARCHIVE=true
- fi
- shift
- done
- cd $WDIR
- echo -n "Working in "
- pwd
- $touch .mclancher_lastmodified
- CURVERSION=`$cat .mclancher_lastmodified`
- echo "Checking HTTP Headers to see if their version is different..."
- $curl -sIL $SERVER_URL > /tmp/mclauncher
- VERSION=`$grep 'Last-Modified: ' /tmp/mclauncher | $sed 's/^Last-Modified: \([a-zA-Z0-9,: ]*\).*$/\1/'`
- if [[ $python == "" ]]; then
- COMPAT_VERSION=`echo -n $VERSION | $sed 's/ /_/g' | $sed 's/:/./g'`
- COMPAT_CURVERSION=`echo -n $CURVERSION | $sed 's/ /_/g' | $sed 's/:/./g'`
- else
- COMPAT_VERSION=`$python -c "from datetime import datetime; print datetime.strptime('$VERSION', '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y-%m-%dT%H.%I.%S')"`
- COMPAT_CURVERSION=`$python -c "from datetime import datetime; print datetime.strptime('$CURVERSION', '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y-%m-%dT%H.%I.%S')"`
- fi
- $rm /tmp/mclauncher
- if [[ $VERSION != $CURVERSION ]] || [ ! -f minecraft_server.jar ]; then
- if [ -f minecraft_Server.jar ]; then
- echo "The server has been modified! Lets get it!"
- else
- echo "There is no minecraft_server.jar, so lets get one!"
- fi
- echo -n $VERSION > .mclancher_lastmodified
- $curl $SERVER_URL -so minecraft_server_$COMPAT_VERSION.jar
- $sha1sum minecraft_server_$COMPAT_VERSION.jar > minecraft_server_$COMPAT_VERSION.jar.sha1sum
- if [ -f minecraft_server.jar ]; then
- $rm minecraft_server.jar
- fi
- $ln -s minecraft_server_$COMPAT_VERSION.jar minecraft_server.jar
- else
- echo "Already upto date."
- fi
- SUM=`$cat "minecraft_server_$COMPAT_VERSION.jar.sha1sum"`
- CURSUM=`$cat "minecraft_server_$COMPAT_CURVERSION.jar.sha1sum"`
- if [[ ${SUM:0:40} == ${CURSUM:0:40} ]]; then
- echo "The file is different, this was probably an update."
- if $ARCHIVE && [ -f server.properties ]; then
- WORLD=`$grep 'level-name' server.properties`
- WORLD=${WORLD:11}
- $mkdir -p backups
- $tar -cjf "backups/$WORLD.pre-$COMPAT_VERSION.tar.bz2" $WORLD
- echo 'Created world archive:'
- echo " backups/$WORLD.pre-$COMPAT_VERSION.tar.bz2"
- fi
- fi
- if $START; then
- echo "Starting server..."
- exec $java $START_PARAMS -jar minecraft_server.jar nogui
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement