#!/bin/bash

################################################################
#                                                              #
#                   Minecraft Server Manager                   #
#                                                              #
#                            version 0.1                       #
#                                                              #
#           Author: Marcin Herda <mherda@slackword.net>        #
#                                                              #
################################################################


# Minecraft server directory
S_DIR=~/minecraft

# Worlds' directory
W_DIR="worlds"

# Backup directory
B_DIR=backup

# Server Properties
S_PROP=server.properties


# Temporary file
tf=/tmp/dialog_$$

# Chosen World
ch_world=/tmp/chosen_world



select_world() {

    for i in $S_DIR/$W_DIR/*
    do
	# Store the directories in a tmp file
	echo $i  | awk -F'/' '{ print $NF }' >> $ch_world
    done 


    dialog --backtitle "Minecraft Server Manager" \
	--title "Which world would you like to live in today?" \
	--menu "Select the world" 14 55 12 \
	$(awk '{ print FNR "\t" $1 }' $ch_world) 2> $tf
    
    num_world=$(cat $tf)
    ch_w=$(sed -n "$num_world p" $ch_world) && rm -rf $tf $ch_world
    sed -i "/level-name/ s/\(.[^=]*\)=\(.*\)/\1=$W_DIR\/$ch_w/" $S_DIR/$S_PROP \
	&& rm -rf $ch_world
    play
}

play() {
    
    # Do you want to play a world
    dialog --title "Confirmation" --yesno "Do you want to start the server?" 5 55
    if [ $? -ne 0 ]; then
        main_menu
    else
        start_server
    fi

}

create_world() {
    dialog --title "Create a new world" \
	--backtitle "Minecraft Server Manager" \
	--inputbox "Enter the name of a new world" 8 55 2> $tf
    
    if [ $? -ne 0 ]; then
	dialog --infobox "\nYour world has not been created" 5 55 ; sleep 2
	main_menu
    fi
    
    world_name=$(cat $tf) && rm $tf # TODO deal with spaces in world name
    
    # Check if such a directory already exists.
    if [ -d "$S_DIR/$W_DIR/$world_name" ]; then
	dialog --infobox "\nThis world already exists! Try again" 5 55 ; sleep 2
	create_world
    else
	# Create the world directory and set the current world in the server properties file.
	mkdir $S_DIR/$W_DIR/$world_name \
	    && sed -i "/level-name/ s/\(.[^=]*\)=\(.*\)/\1=$W_DIR\/$world_name/" $S_DIR/$S_PROP \
	    || echo "Couldn't create the directory" 
    fi
    
    # Do you want to play the new world
    play
}

start_server() {
   
    cd $S_DIR && ./minecraft.sh
    main_menu 

}

backup_worlds() {

    if [ ! -d "$S_DIR/$B_DIR" ]; then
	mkdir $S_DIR/$B_DIR
    fi

    dialog --infobox "Backing up worlds, please wait" 3 55
    cp -r $S_DIR/$W_DIR/* $S_DIR/$B_DIR \
	&& dialog --infobox "Operation complete" 3 55 ; sleep 1
    
    main_menu
    
}

main_menu () {

    dialog --title "Minecraft Server Manager" \
	--menu "Please choose an option:" 15 55 5 \
	1 "Select a world" \
	2 "Create a new world" \
	3 "Start the server" \
	4 "Backup Worlds" \
	5 "Exit" 2> $tf
    
    if [ $? -ne 0 ]; then
	echo "Exiting..."
	exit
    fi

    choice=$(cat $tf) && rm $tf
    case $choice in
	1) select_world
	    ;;
	2) create_world
	    ;;
	3) play
	    ;;
	4) backup_worlds
	    ;;
	4) exit ;;
    esac

}


main_menu
