#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2007 Pâris Quentin
# Copyright (C) 2010 PETIT Aymeric

# 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. 

#Variables
die() ## PHP function copy
{
	echo "$@"
	exit
}
[ "$PLAYONLINUX" = "" ] || die "You are already in a PlayOnLinux environement" 
#si root -> exit
[ `id -u` -eq 0 ] && die "This software can't be used as root"
#Dans quel répértoire se trouve le script ?
PLAYONLINUX=`dirname "$0"`
cd "$PLAYONLINUX"
export PLAYONLINUX=`pwd`
#used for switch from gui to a text system.
#allow installing software w/o X server.
export POL_CMD=true
source "$PLAYONLINUX/lib/sources"
if [ "$1" = "--version" ]
then
	echo "PlayOnLinux Command Line $VERSION"
	exit 0
fi

echo "PlayOnLinux Command Line v$VERSION"
echo ""
if [ ! "$(which $PYTHON)" ]
then
	missing "$PYTHON"
fi
function usage
{
	echo "$(eval_gettext 'This program is intended to be used by automatic scripts to manage PlayOnLinux.')"
	echo "Usage: $0"
	echo -e "\t[--version]			$(eval_gettext '	print current version and exit.')"
	echo -e "\t[--update]			$(eval_gettext '	update the PlayOnLinux script repository.')"
	echo -e "\t[--run 'Software name']	$(eval_gettext '	run an existing application.')"
	echo -e "\t[--start-install 'Script Name']$(eval_gettext '	start installing the specified script.')"
	echo -e "\t[--search 'Script Name']	$(eval_gettext '	perform search into the script repository.')"
	echo -e "\t[--list]			$(eval_gettext '	list software installed with PlayOnLinux.')"
	echo -e "\t[--remove 'Script Name']	$(eval_gettext '	remove an existing application.')"
	exit 1
}
#parsing switch
case $1 in
-u|--update)
	#build personnal repository 
	construire_repertoire_personnel
	if [ -z "$2" ] 
	then	
		#update list of script
		if [ -d "$HOME/.PlayOnLinux/configurations/listes" ]
		then
			bash "$PLAYONLINUX/bash/check_maj_"
			echo $?
			exit
		fi
		maj_check
	elif [ "$2" == "--force" ]
	then
		maj_check
	fi
	;;

--run)
	#pour ne pas reinventer la roue ... 
	if [ -z "$2" ] ; then usage ; fi
	#already done in start_pol
	start_pol "@"
	;;
-si|--start-install)
	#deja
	if [ -z "$2" ] ; then usage ; fi
	construire_repertoire_personnel
	#fun function!
	if [ "${2:0:1}" == "/" ]
	then
		#local script
		if [ -e "$2" ] 
		then
			#il existe donc on lance
			bash "$2"
			exit
		fi
		#si on est ici c'est que c'était pas bon donc on teste une nouvelle methode
	fi
	#script install
	if [ "$2" == "install" ]
	then
		
		if [ -e "$REPERTOIRE/install" ]
		then
			bash $REPERTOIRE/install
		fi
		exit
	fi

	#voila voila ...
	name=${2//" "/"%20"}
	rm -f $REPERTOIRE/install 2>/dev/null
	wget -q "$SITE/V3_data/repository/get_file.php?version=playonlinux-$VERSION&id=$name" -O $REPERTOIRE/install || die "$(eval_gettext 'Please make sure you are connected to the Internet.')"
	if [ -e "$REPERTOIRE/install" ]
	then
		bash $REPERTOIRE/install
	fi
	;;
--search)
	if [ -z "$2" ] ; then usage ; fi
	#build personnal repository 
	construire_repertoire_personnel
	echo -e "$(eval_gettext "The search needs an updated repository.\nIf the results seem outdated, please run an update.")"
	echo "$(eval_gettext "Search result(s): ")"
	grep -i -- "$2" $REPERTOIRE/configurations/listes/search
	;;
-ls|--list)
	#build personnal repository 
	construire_repertoire_personnel
	echo "$(eval_gettext "Currently installed applications:")"
	ls -1 $REPERTOIRE/configurations/installed/
	;;
-rm|--remove)
	if [ -z "$2" ] ; then usage ; fi
	#build personnal repository 
	construire_repertoire_personnel
	$PLAYONLINUX/bash/uninstall "$2"
	;;
*)
	usage
	;;
esac
echo ""
exit 0
