#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2011 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
die() ## PHP function copy
{
	echo "$@"
	exit
}

if [ "$1" == "--version" ]
then
	cd "$(dirname "$0")"
	version="$(cat python/lib/Variables.py | grep 'os.environ\["VERSION"\] = ' | cut -d = -f2 | tr -d \" | tr -d " ")"
	die "PlayOnLinux $version"
fi

[ "$(uname -s)" = "Linux" ] || die "This script must be run on a linux system"

export PYTHON="python"
export POL_OS="Linux"
export UBUNTU_MENUPROXY=0 
export MACHTYPE
export WorkingDirectory="$PWD"

[ "$PLAYONLINUX" = "" ] || die "You are already in a PlayOnLinux environement" 

# On part a la recherche de python. (Pourquoi est-ce que certaines distros ne le mette pas dans /usr/bin/python, comme tout le monde :'( )
if [ "$(which $PYTHON)" ]
then
	PythonBin="$PYTHON"
	PythonFound="True"
fi

if [ ! "$PythonFound" = "True" ]
then
	if [ "$(which python2)" ]
	then
		PythonBin="python2"
		PythonFound="True"
	fi
fi

if [ ! "$PythonFound" = "True" ]
then
	if [ "$(which python2.6)" ]
	then
		PythonBin="python2.6"
		PythonFound="True"
	fi
fi

if [ ! "$PythonFound" = "True" ]
then
	if [ "$(which python2.7)" ]
	then
		PythonBin="python2.7"
		PythonFound="True"
	fi
fi


if [ ! "$PythonFound" = "True" ]
then
	echo "Please install python before trying to run PlayOnLinux"
	exit
fi

if [ "$1" == "--run" ]
then
	cd "$(dirname "$0")"
	shift
	NAME="$1"
	shift
	./playonlinux-bash "$HOME/.PlayOnLinux/shortcuts/$NAME" "$@"
else
	cd "$(dirname "$0")/python"
	"$PythonBin" mainwindow.py "$@"
	if [ ! "$?" = "0" ]
	then
		python2.6 mainwindow.py "$@"
	fi
fi
exit $?
