#!/bin/bash

# Copyright (C) 2007 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. 

#Variables

#Dans quel répértoire se trouve le script ?


OLDDIR="$(pwd)"
if [ "$PLAYONLINUX" = "" ]
then
	PLAYONLINUX=`dirname "$0"`
	cd "$PLAYONLINUX"
	export PLAYONLINUX=`pwd`
fi

source "$PLAYONLINUX/lib/sources"


LNG_PKG_NOT_VALID="$(eval_gettext 'The file is not a valid PlayOnLinux package')"
LNG_PKG_NOT_FOUND="$(eval_gettext 'Unable to find the file')"
LNG_PKG_CHOOSE="$(eval_gettext 'Choose a package to install')"
LNG_PKG_MANAGER="$(eval_gettext 'PlayOnLinux package manager')"

echo "$LNG_PKG_MANAGER $VERSION"
if [ "$1" = "--version" ]
then
	echo "PlayOnLinux $VERSION"
	exit 0
fi
if [ "$3" = "-g" -o "$3" = "--gui" ]
then
	error_c="error"
else
	error_c="echo"
fi
if [ "$1" = "-h" -o "$1" = "--help" -o "$1" = "" ]
then
	echo "Usage :"
	echo "-b or --browse		Choose a file"
	echo "-e or --extract 	Extract the package"
	echo "-i or --install		Install the package"
	echo "-h or --help		Show this message"
fi
if [ "$1" = "-b" -o "$1" = "--browse" ]
then
	FILE=`select_file "$LNG_PKG_CHOOSE" '' 0 0 1 "$LNG_PKG_MANAGER"`
	if [ ! $FILE = "" ]
	then
		bash "$PLAYONLINUX/playonlinux-pkg" -i "$FILE" -g
	fi
	exit
fi
if [ "$1" = "-e" -o "$1" = "--extract" ]
then
	cd "$OLDDIR"
	PACKAGE="$2"
	TMP="$REPERTOIRE/tmp/"
	echo ""
	echo "Opening $2"
	if [ ! -e "$PACKAGE" ]
	then 
		$error_c "$LNG_PKG_NOT_FOUND : $2"
		exit
	fi
	ext=`echo $PACKAGE | awk -F "." '{print $NF}'`
	if [ ! "$ext" = "pol" ]
	then
		$error_c "$LNG_PKG_NOT_VALID : $2"
		exit
	fi
	echo "Extracting..."
	mkdir package
	tar -xvf "$PACKAGE" -C ./package > /dev/null 2> /dev/null
	exit
fi
if [ "$1" = "-i" -o "$1" = "--install" ]
then
	cd "$OLDDIR"
	PACKAGE="$2"
	TMP="$REPERTOIRE/tmp/"
	echo ""
	echo "Opening $2"
	if [ ! -e "$PACKAGE" ]
	then 
		$error_c "$LNG_PKG_NOT_VALID : $2"
		exit
	fi
	ext=`echo $PACKAGE | awk -F "." '{print $NF}'`
	if [ ! "$ext" = "pol" ]
	then
		$error_c "$LNG_PKG_NOT_VALID : $2"
		exit
	fi
	rm "$TMP/pkg/" -r 2> /dev/null
	mkdir "$TMP/pkg/" -p
	cp "$2" "$TMP/pkg"
	cd "$TMP/pkg"
	echo "Extracting $2..."
	tar -xvf *.pol > /dev/null 2> /dev/null	
	if [ "$?" = "2" ]
	then
		$error_c "$LNG_PKG_NOT_VALID : $2"
		exit
	fi
	if [ ! -e "$TMP/pkg/playonlinux/main" ]
	then
		$error_c "$LNG_PKG_NOT_VALID : $2"
		exit
	fi
	echo "Running ..."
	export SCRIPT_DIRECTORY="$TMP/pkg/files/"
	bash "$TMP/pkg/playonlinux/main"
	rm "$TMP/pkg/" -r 2> /dev/null
	echo "Cleaning ..."
fi
exit 0
