#!/bin/sh
### BEGIN INIT INFO
# Provides:          minetestserver
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Minetest server
# Description:       Minetest server with backup and ramdisk
### END INIT INFO

# /etc/init.d/minetestserver
# Author: Andrey

#Settings
SERVICE="minetestserver"
WORLD="justtest"
USERNAME="user"
MINETESTFOLDER="/home/$USERNAME/.minetest"
WORLDFOLDER="/home/$USERNAME/.minetest/worlds/$WORLD"
INVOCATION="/usr/games/$SERVICE"
PIDFILE=/var/run/$SERVICE.pid
OPTIONS="--worldname $WORLD"
 
# Exit if the package is not installed
[ -x "$INVOCATION" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$SERVICE ] && . /etc/default/$SERVICE
 
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

mt_start() {
    echo "Backup players and map... "
    cp -rp $WORLDFOLDER/players.safe/* $WORLDFOLDER/players.bac/
    cp -rp $WORLDFOLDER/auth.txt $WORLDFOLDER/auth.bac/
    cp -rp $WORLDFOLDER/map.safe/map.sqlite $WORLDFOLDER/map.bac/map.sqlite
    
    if [ -f "$MINETESTFOLDER/clear_players.py" ]; then
        echo "Clearing players..."
        python $MINETESTFOLDER/clear_players.py
    fi

    echo "Starting ramdisk 1, 2 ... "
    mount -t ramfs -o size=100M ramfs $WORLDFOLDER/players
    chown $USERNAME:$USERNAME $WORLDFOLDER/players/
    mount -t ramfs -o size=100M ramfs $MINETESTFOLDER/debug
    chown $USERNAME:$USERNAME $MINETESTFOLDER/debug/
    cp -rp $WORLDFOLDER/players.safe/* $WORLDFOLDER/players/

    echo "Starting ramdisk 3 ..."
    mount -t ramfs -o size=1G ramfs $WORLDFOLDER/map
    chown $USERNAME:$USERNAME $WORLDFOLDER/map/
    cp -rp $WORLDFOLDER/map.safe/map.sqlite $WORLDFOLDER/map/map.sqlite
    echo "Starting $SERVICE...."
    start-stop-daemon --start --chuid "$USERNAME:$USERNAME" --background --make-pidfile --pidfile $PIDFILE --exec $INVOCATION -- $OPTIONS
    echo "Done."
}
 
mt_stop() {
    echo "Stopping $SERVICE... "
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $SERVICE	
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $INVOCATION
    rm -f $PIDFILE

    echo "Syncing ramdisk 1, 2 ... "
    rm -f $WORLDFOLDER/players/*.~mt
    cp -rp $WORLDFOLDER/players/* $WORLDFOLDER/players.safe/
    umount $WORLDFOLDER/players
    cp -rp $MINETESTFOLDER/debug/debug.txt $MINETESTFOLDER/debug.safe/debug`date +%F`.txt
    umount $MINETESTFOLDER/debug

    echo "Syncing ramdisk 3 ... "
    cp -rp $WORLDFOLDER/map/map.sqlite $WORLDFOLDER/map.safe/map.sqlite
    umount $WORLDFOLDER/map
    echo "Done."
}

mt_pause() {
    kill "$(pidof $SERVICE)"
    echo "Done!"
}

mt_continue() {
    echo "Restoring $SERVICE...."
    if [ ! -f "$PIDFILE" ]; then
        echo "Not started!"
    else
        if [ "$(pidof $SERVICE)" ]; then
            echo "Already running!"
        else
            start-stop-daemon --start --chuid "$USERNAME:$USERNAME" --background --make-pidfile --pidfile $PIDFILE --exec $INVOCATION -- $OPTIONS
            echo "Done!"
        fi
    fi
}
 
mt_status() {
    status_of_proc -p $PIDFILE "" "$SERVICE" && exit 0 || exit $?
}
 
 
#Start-Stop here
case "$1" in
start)
mt_start
;;
stop)
mt_stop
;;
pause)
mt_pause
;;
continue)
mt_continue
;;
status)
mt_status
;;
 
*)
echo "Usage: /etc/init.d/minetest {start|stop|pause|continue|status}"
exit 1
;;
esac
 
exit 0
