#!/bin/bash

# Copyright (C) 2010 Pâris Quentin

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 

[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"

TITLE="Offline PlayOnLinux"
mkdir -p "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/"
export CFILE="$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/plugin.cfg"
source "$CFILE"
if [ "$MODE" = "" ]
then
	POL_SetupWindow_Init
	POL_SetupWindow_message "Offline PlayOnLinux is not configured ! Go to options menu to configure it"
	POL_SetupWindow_Close
	exit
fi

start_server()
{
	cd "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux"
	cd "PlayOnLinux_Server"
	./PlayOnLinux_Server.py &
}
stop_server()
{
	killall PlayOnLinux_Server.py
}
restart_server()
{
	stop_server
	start_server
}
update_server()
{
	cd "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/PlayOnLinux_Server"
	POL_SetupWindow_download "Downloading all PlayOnLinux scripts ..." "$TITLE" "http://repository.playonlinux.com/PlayOnLinux_Scripts.tar.gz"
	POL_SetupWindow_wait_next_signal "Extracting the scripts ..." "$TITLE"
	sleep 1
	rm PlayOnLinuxData -r 2> /dev/null
	tar -xvf PlayOnLinux_Scripts.tar.gz
	POL_SetupWindow_detect_exit
	restart_server
}
POL_SetupWindow_Init
POL_SetupWindow_free_presentation "$TITLE" "Welcome to Offline PlayOnLinux plugin\n\nThis wizard will help you to manage your server"

while [ 1 ]
do
	POL_SetupWindow_menu "What do you want to do ?" "$TITLE" "Start the server/Stop the server/Restart the server/Update and restart the server/Close this window" "/"
	
	ACTION=$APP_ANSWER
	
	if [ "$ACTION" = "Start the server" ]
	then
		start_server
	fi
	
	if [ "$ACTION" = "Stop the server" ]
	then
		stop_server
	fi
	
	if [ "$ACTION" = "Restart the server" ]
	then
		restart_server
	fi
	
	if [ "$ACTION" = "Update and restart the server" ]
	then
		update_server
	fi

	if [ "$ACTION" = "Close this window" ]
	then
		POL_SetupWindow_Close
		exit
	fi
	POL_SetupWindow_wait_next_signal "Please wait" "$TITLE"
	sleep 1
	POL_SetupWindow_detect_exit
done


