#!/bin/bash
# Copyright (C) 2007 Pâris Quentin
#  		     Cassarin-Grand Arthur

# 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. 
maj_check () 
{
		clean_tmp
		cd $REPERTOIRE
		if [ -e "$HOME/.PlayOnLinux/configurations/options/community" ]
		then
			wget -q "$SITE/liste_v2/jeux.php?community=1" -O liste_jeux 
			wget -q "$SITE/liste_v2/expansions.php?community=1" -O liste_expansions
			wget -q "$SITE/liste_v2/patchs.php?community=1" -O liste_patchs
		else
			wget -q "$SITE/liste_v2/jeux.php" -O liste_jeux
			wget -q "$SITE/liste_v2/expansions.php" -O liste_expansions
			wget -q "$SITE/liste_v2/patchs.php" -O liste_patchs
		fi
		if [ -e "$REPERTOIRE/configurations/options/work" ]
		then
			wget -q "$SITE/liste_v2/work.php" -O liste_work
		fi
}
clean_repertoires ()
{
	rm $REPERTOIRE/jeux -rf
	rm $REPERTOIRE/expansions -rf
	rm $REPERTOIRE/patchs -rf
}
menu_principal()
{
	if [ ! -e "$REPERTOIRE/icones/32/" ]
	then
		mkdir -p $REPERTOIRE/icones/32/
		convert -resize 32 "$PLAYONLINUX/etc/playonlinux.png" "$REPERTOIRE/icones/32/playonlinux.png"
	fi
	if [ ! -e "$REPERTOIRE/configurations/v2" ]
	then
		bash $PLAYONLINUX/bash/check_maj
		touch $REPERTOIRE/configurations/v2
	fi
	python2.5 "$PLAYONLINUX/python/mainwindow.py"
}
install_fonts ()
{
	if [ ! -e "$HOME/.PlayOnLinux/configurations/fonts_installed" ]
	then
		message "$LNG_MAIN_INSTALLFONTS"
		cd $HOME/.PlayOnLinux/tmp
		telecharger "$LNG_MAIN_DOWNLOADFONTS" "$SITE/divers/polices.tar.gz" 
		cd $HOME/.PlayOnLinux/
		tar -xvf ./tmp/polices.tar.gz
		touch configurations/fonts_installed
	fi
}
construire_repertoire_personnel ()
{
	mkdir $HOME/.PlayOnLinux/wineprefix/ -p
	mkdir $HOME/.PlayOnLinux/configurations/installed/ -p
	mkdir $HOME/.PlayOnLinux/configurations/options/ -p
	mkdir $HOME/.PlayOnLinux/tmp -p
	mkdir $HOME/.PlayOnLinux/icones/ -p
	mkdir $HOME/.PlayOnLinux/WineVersions -p
}
first_use ()
{
	clean_tmp
	message "$LNG_MAIN_FIRSTUSE"
	construire_repertoire_personnel
	install_fonts
	menu_principal
}
start_pol ()
{
	if [ ! -e "$REPERTOIRE/configurations/options/" ]
	then
		first_use
		exit $EXIT_SUCCES
	fi	

	if [ "$1" = "--run" ]
	then
		#Arg contient tout apres l'option --run
		#Supporte le lancement de jeu avec option POL >> 1.7.5
		Arg=$(echo $@ | cut -b7-)
		Arg=${Arg//"$2"/""}
		lancer "$2" "$Arg"
		exit 0
	fi
	check_new_pol&
	menu_principal
}
clean_tmp ()
{
	rm $REPERTOIRE/tmp/ -rf
	mkdir $REPERTOIRE/tmp -p
}
check_network ()
{
	#Ecrit par MulX
	#Le 23/sep/2007 
	TMPFILE=$(mktemp)
	COUNT=1
	QUIET="--quiet"
	NetStat="yes"
	if [ -n "$1" ]
	then 
		PING="$1"
	else
		PING="google.com"
	fi
	#( 
	#	echo 0
		ping $PING -c $COUNT &> $TMPFILE 
	#	echo 100
	#) | attendre_multiple "$LNG_NET_CHECK"
	if [ -n "$(grep "unknown host" "$TMPFILE" )" ]
	then 
		if [ "$2" != "$QUIET" ]
		then
			erreur "$LNG_NET_CHECK_FAILED"
		fi
		NetStat="no"
		return 255
	fi
	Received=$(grep "received" $TMPFILE | awk '{printf("%s",$4)}' )
	rm -f $TMPFILE
	if [ $Received == 0 ]
	then 
		if [ "$2" != "$QUIET" ]
		then
			erreur "$LNG_NET_CHECK_FAILED"
		fi
		NetStat="no"
		return 255
	elif [ $Received != $COUNT ]
	then
		if [ "$2" != "$QUIET" ]
		then
			message "$LNG_NET_CHECK_LOST"
		fi
		NetStat="yes"
	fi
	if [ "$NetStat" == "yes" ]
	then	
		if [ "$2" != "$QUIET" ]
		then
			message "$LNG_NET_CHECK_SUCCESS"
		fi
		return 0
	fi
	return 255
}
fixme () 
{
	#generic function for all FIXME
	#PLEASE DO NOT TRANSLATE ANY OF THIS PART !
	#call this function by 'fixme "path to file" "function_to_fix" "comment of fix to do"'
	local IFS='::'
	echo "FIXME::POL::$*"
}
check_new_pol () 
{
	#Check if a new version of POL is avaible
	#check_network "www.playonlinux.com" --quiet
	#if [ $? != 0 ]
	#then
	#	erreur "$LNG_NET_POL_ERR"
	#	return 2
	#fi
	WebVersion=$(wget -q $SITE/version2.php -O-)
	if [ "$WebVersion" = "" ]
	then
		erreur "$LNG_NET_POL_ERR"
		return 2
	fi
        if [ "$WebVersion" != "$VERSION" ]
	then
		message "$LNG_MAIN_POL_UPDATE ($WebVersion)"
		return 1
	fi
	return 0
}
