#!/bin/sh

# Copyright (C) Takashi Hirata <webmaster at up-cat.net>
# This is a part of felidlabo MinGW pack.
#   http://up-cat.net/

echo "INFO: git install scripts."

. /etc/profile
. /etc/flenv

dlpath=$flpath/git
expath=$flpath/git/curl
curlurl=http://www.gknw.net/mirror/curl/win32/curl-7.20.1-devel-mingw32.zip
giturl=http://kernel.org/pub/software/scm/git/git-1.7.0.6.tar.bz2

curlarc=`basename $curlurl`
gitarc=`basename $giturl`

mkdir -p $dlpath

wget $curlurl -t 3 -T 30 -N -P $dlpath || { echo "ERROR: failed to download curl" ; exit 1 ; }
wget $giturl -t 3 -T 30 -N -P $dlpath || { echo "ERROR: failed to download git" ; exit 1 ; }

mkdir -p expath=$flpath/git/curl

pushd $dlpath
#$sevenzapath x $curlarc || { echo "ERROR: failed to extract curl" ; exit 1 ; }
#tar -jxvf $gitarc || { echo "ERROR: failed to extract git" ; exit 1 ; }

gitdir=`find . -maxdepth 1 -type d -name git-\*`
curldir=`find . -maxdepth 1 -type d -name curl-\*`

if [ -d "$gitdir" -a -d "$curldir" ]; then

	# we should use libz in mingw
	rm $curldir/lib/libz* > /dev/null 2>&1

	# fix Makefile not to use -g
	pushd $gitdir
	if [ ! -f Makefile.org ]; then
		mv -f Makefile Makefile.org
	fi
	perl -pe 's/CFLAGS\s*=\s*-g\s+/CFLAGS = /' Makefile.org > Makefile

	# git commands uses iconv and it's huge to link statically.
	# we use dll verision
	$flpath/bin/dlliconv.sh dll

	# go ahead
	cmd_arg="prefix=$dlpath NO_OPENSSL=YesPlease NO_TCLTK=YesPlease"
	{ CFLAGS=-I$dlpath/$curldir/include LDFLAGS=-L$dlpath/$curldir/lib make $cmd_arg all \
	&& strip *.exe \
	&& CFLAGS=-I$dlpath/$curldir/include LDFLAGS=-L$dlpath/$curldir/lib make $cmd_arg install \
	; } \
	|| { echo "ERROR: failed to build git" ; }

	# restore iconv
	$flpath/bin/dlliconv.sh static

	popd
fi

popd
