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

if [ "$PLAYONLINUX" = "" ]
then
exit 0
fi
source "$PLAYONLINUX/lib/sources"
cfg_check

message "$LNG_GIT_WELCOME1\n\n$LNG_GIT_WELCOME2" "Wine GIT" 0 0 1

# Check
check_one git
check_one gcc
check_one make
check_one flex
check_one bison

# Check environnement
if [ "$MACHTYPE" != "x86_64-pc-linux-gnu" ]; then
	GIT_TEST_ENV=ia32
else
	GIT_TEST_ENV=amd64
fi

# Preparations
GIT_TEMP=$HOME/.PlayOnLinux/tmp/git/
GIT_WINEVERSION=$HOME/.PlayOnLinux/WineVersions/git/

if [ -e "$GIT_TEMP" ]; then
	attendre "$LNG_WAIT" "rm -rf $GIT_TEMP" "" 0 0 0 "" 1 
fi
if [ -e "$GIT_WINEVERSION" ]; then
	GIT_KEEP=$(champ "$LNG_GIT_KEEP" "N" ) ;
	if [ "$GIT_KEEP" == "Y" ]; then
		GIT_RENAME=$(champ "$LNG_GIT_RENAME" "" 1 1 1 "WineGIT" ) ;
		if [ "$GIT_RENAME" == "" ]; then
			erreur "$LNG_GIT_NAME_ERROR"
			exit 0
		else if [ -e "$GIT_RENAME" ]; then
			erreur "$LNG_GIT_NAME_DUPLICATE_ERROR"
			until [ ! -e "$GIT_RENAME" ];
			do
				GIT_RENAME=$(champ "$LNG_GIT_KEEP2" "" 1 1 1 "WineGIT" ) ;
			done
		else
			mv $GIT_WINEVERSION $HOME/.PlayOnLinux/WineVersions/$GIT_RENAME
			chmod 777 $HOME/.PlayOnLinux/WineVersions/$GIT_RENAME -R
		fi
		fi
	else
		rm -rf $GIT_WINEVERSION
	fi
fi
mkdir -p $GIT_TEMP
mkdir -p $GIT_WINEVERSION
mkdir -p $GIT_WINEVERSION/usr/
chmod 777 $GIT_TEMP -R
chmod 777 $GIT_WINEVERSION -R

cd $GIT_TEMP
attendre "$LNG_GIT_DOWNLOAD" "git clone git://source.winehq.org/git/wine.git wine" "" 1 4 1 "" 1
cd wine
if [ "$GIT_TEST_ENV" == "amd64" ]; then
	attendre "$LNG_GIT_COMPILATION" "LDFLAGS=\"-L/lib32 -L/usr/lib32 -Wl,-rpath,/lib32 -Wl,-rpath,/usr/lib32\" ./configure --prefix=$GIT_WINEVERSION/usr/" "" 2 4 1 "" 1
	attendre "$LNG_GIT_COMPILATION" "make depend && make" "" 3 4 1 "" 1
	attendre "$LNG_GIT_COMPILATION" "make install" "" 4 4 1 "" 1
else
	attendre "$LNG_GIT_COMPILATION" "./configure --prefix=$GIT_WINEVERSION/usr/" "" 2 4 1 "" 1
	attendre "$LNG_GIT_COMPILATION" "make depend && make" "" 3 4 1 "" 1
	attendre "$LNG_GIT_COMPILATION" "make install" "" 4 4 1 "" 1
fi

message "$LNG_GIT_FINISHED"
