#!/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"
export POLOFF_LOADED="TRUE"
TITLE="Offline PlayOnLinux configuration"

POL_SetupWindow_Init
POL_SetupWindow_free_presentation "$TITLE" "Welcome to Offline PlayOnLinux plugin\n\nIt will allows you to run to run all the scripts without internet access by running a script server on your computer\nThis wizard will help you to configure Offline PlayOnLinux\nClick on next button to continue"

cp -r "$REPERTOIRE/plugins/Offline PlayOnLinux/PlayOnLinux_Server" "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/PlayOnLinux_Server"

POL_SetupWindow_menu "Choose a mode to configure $TITLE" "$TITLE" "Beginner mode/Expert mode" "/"

MODE=$APP_ANSWER
mkdir -p "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/"
export CFILE="$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/plugin.cfg"

if [ "$MODE" == "Beginner mode" ]
then
	echo "MODE=1" > "$CFILE"
	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

else
	echo "MODE=2" > "$CFILE"
	POL_SetupWindow_question "Do you want to download the official scripts ?\nIf you say no, you will have to put your own scripts in\n$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/PlayOnLinux_Server/PlayOnLinuxData\n\nMoreover, script have to be in one of these subdirectories :\nGames, Accessories, Development, Other, Graphics, Office, Internet, Multimedia, Education,\nFunctions, Testing, Patches" "$TITLE"
	if [ "$APP_ANSWER" = "TRUE" ]
	then
		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
	else
		mkdir -p "$REPERTOIRE/configurations/plugins_conf/Offline PlayOnLinux/PlayOnLinux_Server/PlayOnLinuxData"
	fi
	POL_SetupWindow_question "Do you want to run PlayOnLinux server on this computer ?\nIf you say no, you will have to choose another server\n\nBy answering no, you will be able to run a single server for all you network" "$TITLE"
	if [ "$APP_ANSWER" = "TRUE" ]
	then
		echo "RUN_SERVER=TRUE" >> "$CFILE"
		dhost="localhost"
		dport="8050"
	else
		echo "RUN_SERVER=FALSE" >> "$CFILE"
		dhost="repository.playonlinux.com"
		dport="80"
	fi
	POL_SetupWindow_textbox "What is the name of the server you want to use ? (Host, IP)" "$TITLE" "$dhost"
	echo "SERVER_HOST=$APP_ANSWER" >> "$CFILE"

	POL_SetupWindow_textbox "What is the port of the server you want to use ?" "$TITLE" "$dport"
	echo "SERVER_PORT=$APP_ANSWER" >> "$CFILE"

	
fi


POL_SetupWindow_message "Configuration is finished\n\nIMPORTANT : Don't forget to disable this plugin, and to restart PlayOnLinux to have access\nto the official repository\n\nThis plugin does NOT give access to wine version repository offline\n\nIMPORTANT : You need to restart PlayOnLinux for the changes to take effect" "$TITLE"

POL_SetupWindow_Close

