#!/bin/bash

# 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`
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 for manage PlayOnLinux.')"
	echo "Usage: $0 [--update] [--version] [--run] [--start-install]"
	echo -e "\t[--version] $(eval_gettext 'print current version and exit')"
	echo -e "\t[--update] $(eval_gettext 'use this switch to update PlayOnLinux script repository')"
	echo -e "\t[--run 'Software name'] $(eval_gettext 'use this switch to run an already installed software')"
	echo -e "\t[--start-install 'Script Name'] $(eval_gettext 'start installation a specified script')"
	exit 1
}
#parsing switch
case $1 in
-u|--update)
	#update list of script
	maj_check
	;;

--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
	#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?id=$name -O $REPERTOIRE/install || die "$(eval_gettext 'Please assure you are connected to internet.')"
	if [ -e "$REPERTOIRE/install" ]
	then
		bash $REPERTOIRE/install
	fi
	;;
*)
	usage
	;;
esac
echo ""
exit 0
