Skip to content

Instantly share code, notes, and snippets.

@Fritigern
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fritigern/7654e5115c64e87f5cea to your computer and use it in GitHub Desktop.
Save Fritigern/7654e5115c64e87f5cea to your computer and use it in GitHub Desktop.
A script which provides a simple GUI wrapper for minetestserver
#!/bin/bash
## ###################
## This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
## Read the license terms at http://creativecommons.org/licenses/by-nc-sa/4.0/
## ###################
## You can find the latest version of this script at https://gist.github.com/Fritigern/7654e5115c64e87f5cea
## ###################
## Note that this script depends on Yad, which is a fork of Zenity
## If your distro's repository does not contain Yad, then you can download it from
## http://sourceforge.net/projects/yad-dialog/
## ###################
#Let's have a debug mode, it comes in useful sometimes
DEBUG_MODE=TRUE
# Default values to get you started. Prolly won't be right for you.
instl_path="/home/$USER/Minetest"
executable="$instl_path/bin/minetestserver"
world="$instl_path/worlds/myworld"
game="$instl_path/games/minetest_game"
server_files="$instl_path/worlds/myworld"
logs_path="$instl_path/worlds/myworld/logs"
server_port="30000"
#Read config file
. ~/.mtserv.rc
#I've forgot why i needed this. Leaving it in for now.
cd $instl_path
#Some code that i have found to get a list of subfolders and store them in an array.
#Getting the list of worlds
declare -a worlds
i=1
for d in $instl_path/worlds/*/
do
worlds[i++]="${d%/}"
done
# And now we turn the array into a string, with the exclamation mark as separator.
world_list=$(IFS='!';echo "${worlds[*]}";IFS=$' \t\n')
declare -a games
i=1
for d in $instl_path/games/*/
do
games[i++]="${d%/}"
done
# And now we turn the array into a string, with the exclamation mark as separator.
games_list=$(IFS='!';echo "${games[*]}";IFS=$' \t\n')
# Bunch of functions. Their names are self-explanatory
function start_server {
echo "----------------------" >>$logs_path/debug.txt
echo "Server started at "`date` >>$logs_path/debug.txt
echo "----------------------" >>$logs_path/debug.txt
exec $executable \
--worldname $world \
--config $server_files/minetest.conf \
--gameid $game \
--port $server_port \
--logfile $logs_path 2>&1 | yad --text-info\
--geometry 800x600\
--fore="#000000"\
--back="#ffffff"\
--tail\
--fontname="PT mono, monospace"\
--button="Restart:11"\
--button="Stop:12"
ret=$?
}
export -f start_server
function restart_server {
echo "**** Stopping server ****"
echo " "
killall -s HUP minetestserver
sleep 5
echo "**** Starting server ****"
echo " "
echo "----------------------" >>$logs_path/debug.txt
echo "Server restarted at "`date` >>$logs_path/debug.txt
echo "----------------------" >>$logs_path/debug.txt
exec $executable \
--worldname $world \
--config $server_files/minetest.conf \
--gameid $game \
--port $server_port \
--logfile $logs_path 2>&1 | yad --text-info\
--geometry 800x600\
--fore="#000000"\
--back="#ffffff"\
--tail\
--fontname="PT mono, monospace"\
--button="Restart:11"\
--button="Stop:12"
ret=$?
}
export -f restart_server
function stop_server {
echo "**** Stopping server ****"
echo " "
killall -s HUP minetestserver
exit 0
}
export -f stop_server
function save_config {
echo 'instl_path="'$instl_path'"' > ~/.mtserv.rc
echo 'executable="'$executable'"' >> ~/.mtserv.rc
echo 'world="'$world'"' >> ~/.mtserv.rc
echo 'game="'$game'"' >> ~/.mtserv.rc
echo 'server_files="'$server_files'"' >> ~/.mtserv.rc
echo 'logs_path="'$logs_path'"' >> ~/.mtserv.rc
echo 'server_port="'$server_port'"' >> ~/.mtserv.rc
# eval exec "start_server" # <=== Does not work right now, Not sure why. Using the code below as workaround.
echo "----------------------" >>$logs_path/debug.txt
echo "Server started at "`date` >>$logs_path/debug.txt
echo "----------------------" >>$logs_path/debug.txt
exec $executable \
--worldname $world \
--config $server_files/minetest.conf \
--gameid $game \
--port $server_port \
--logfile $logs_path 2>&1 | yad --text-info\
--geometry 800x600\
--fore="#000000"\
--back="#ffffff"\
--tail\
--fontname="PT mono, monospace"\
--button="Restart:11"\
--button="Stop:12"
ret=$?
}
export -f save_config
# Build the first gui and populate it either with the values from the config, or with default values if there is no config yet.
OUTPUT=$(yad --title="Minetest Server Monitor"\
--text=" Simple Minetest Server Monitor"\
--geometry=600x300\
--separator="!"\
--form\
--field="Install Path" $instl_path\
--field="Executable:FL" "$instl_path/bin/minetestserver"\
--field="World:CB" $world_list\
--field="Game:CB" $games_list\
--field="Server Files" $server_files\
--field="Logs Path" "$logs_path"\
--field="Port" $server_port\
--button="Save config:20"\
--button="Start:10" \
--button="gtk-cancel:99"
)
ret=$? # Get return code, and split that into variables.
instl_path=$(awk -F! '{print $1}' <<<$OUTPUT)
executable=$(awk -F! '{print $2}' <<<$OUTPUT)
world=$(basename $(awk -F! '{print $3}' <<<$OUTPUT))
game=$(basename $(awk -F! '{print $4}' <<<$OUTPUT))
server_files=$(awk -F! '{print $5}' <<<$OUTPUT)
logs_path=$(awk -F! '{print $6}' <<<$OUTPUT)
server_port=$(awk -F! '{print $7}' <<<$OUTPUT)
# If the DEBUG_MODE is active, then we will show the contents of the variables in the terminal
if [ $DEBUG_MODE == TRUE ]; then
echo "OUTPUT = $OUTPUT"
echo "executable = $executable"
echo "instl_path = $instl_path"
echo "world = $world"
echo "game = $game"
echo "server_files = $server_files"
echo "logs_path = $logs_path"
echo "server_port = $server_port"
fi
# Now we execute the functions, based on which button was clicked.
while true
do
case $ret in
10*) "start_server" ;;
11*) "restart_server" ;;
12*) "stop_server" ;;
20*) "save_config" ;;
*) echo "exit $?" ; exit 1 ;;
esac
eval exec $cmd
done
# Quit, Snagglepuss style!
stageleft=0
exit $stageleft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment