#!/bin/sh

if [ "$#" -lt 3 ];
then
	echo "Usage: install_lazarus_solaris.sh <i386/x86_64/sparc> <fpc version> <lazarus version> [lazarus source revision]"
	exit 0
fi

case "$1" in
	"i386")
		carch="386"
	;;
	"x86_64")
		carch="x64"
	;;
	"sparc")
		carch="sparc"
	;;
	*)
		echo "Invalid architecture."
		exit 0
	;;
esac

if [ ! -f "/opt/csw/bin/pkgutil" ];
then
	pkgadd -d http://get.opencsw.org/now
fi
PATH="/opt/csw/bin:""$PATH"

pkgutil -U
pkgutil -y -i wget
pkgutil -y -i coreutils
pkgutil -y -i binutils
pkgutil -y -i gtar
pkgutil -y -i gzip
pkgutil -y -i gmake
pkgutil -y -i gdb
pkgutil -y -i libgtk2_dev

cd /tmp

if [ ! -f "/usr/bin/ppc""$carch" ];
then
	DNAME="fpc-""$2"".""$1""-solaris"
	ANAME="$DNAME"".tar"
	wget "ftp://ftp.hu.freepascal.org/pub/fpc/dist/""$2""/""$1""-solaris/""$ANAME"
	if [ ! -f "$ANAME" ];
	then
		echo "Could not download FreePascal binaries."
		exit 0
	fi
	tar -xvf "$ANAME"
	rm "$ANAME"
	cd "$DNAME"
	echo "Install to /usr/ instead of /usr/local/"
	./install.sh
	cd ..
	rm -rf "$DNAME"
fi

cd /tmp

if [ ! -f "/usr/share/fpcsrc/""$2""/Makefile" ];
then
	ANAME="fpc-""$2"".source.zip"
	wget "https://netcologne.dl.sourceforge.net/project/freepascal/Source/""$2""/""$ANAME" --no-check-certificate
	if [ ! -f "$ANAME" ];
	then
		echo "Could not download FreePascal sources."
		exit 0
	fi
	unzip "$ANAME"
	rm "$ANAME"
	SDNAME="fpc-""$2"
	DDNAME="/usr/share/fpcsrc/""$2""/"
	mkdir -p "$DDNAME"
	mv "$SDNAME"/* "$DDNAME"
	rm -rf "$SDNAME"
fi

cd /tmp

if [ ! -f "/usr/local/share/lazarus/lazarus" ];
then
	if [ ! -d "lazarus" ];
	then
		REV=""
		if [ "$4" != "" ];
		then
			REV="-""$4"
		fi
		ANAME="lazarus-""$3""$REV"".zip"
		wget "https://master.dl.sourceforge.net/project/lazarus/Lazarus%20Zip%20_%20GZip/Lazarus%20""$3""/""$ANAME" --no-check-certificate
		if [ ! -f "$ANAME" ];
		then
			echo "Could not download Lazarus sources."
			exit 0
		fi
		unzip "$ANAME"
		rm "$ANAME"
	fi
	cd lazarus
	if [ ! -f "lazarus.Makefile.solaris.diff" ];
	then
		wget http://oscomp.hu/depot/lazarus.Makefile.solaris.diff
		patch < lazarus.Makefile.solaris.diff
	fi
	if [ ! -f "install/man/lazarus.man.Makefile.solaris.diff" ];
	then
		cd install/man
		wget http://oscomp.hu/depot/lazarus.man.Makefile.solaris.diff
		patch < lazarus.man.Makefile.solaris.diff
		cd ../..
	fi
	gmake clean bigide
	if [ -f lazarus ];
	then
		CP=/bin/cp gmake install # /opt/csw/gnu/cp fails with "preserving permissions for /path/to/file: Invalid argument." Solaris base cp used
	fi
	cd ..
	if [ -f "/usr/local/share/lazarus/lazarus" ];
	then
		rm -rf lazarus
	fi
fi

case "$1" in
	"i386")
		if [ ! -d /usr/lib/32.bak ];
		then
			mv /usr/lib/32 /usr/lib/32.bak
			ln -s /opt/csw/lib/i386 /usr/lib/32
			ln -s /usr/lib/libX11.so.4 /usr/lib/32/
			ln -s /usr/lib/libXinerama.so.1 /usr/lib/32/
			ln -s /usr/lib/libmlib.so.2 /usr/lib/32/
		fi
	;;
	"x86_64")
		if [ ! -d /usr/lib/64.bak ];
		then
			mv /usr/lib/64 /usr/lib/64.bak
			ln -s /opt/csw/lib/amd64 /usr/lib/64
			ln -s /usr/lib/amd64/libX11.so.4 /usr/lib/64/
			ln -s /usr/lib/amd64/libXinerama.so.1 /usr/lib/64/
			ln -s /usr/lib/amd64/libmlib.so.2 /usr/lib/64/
		fi
	;;
	"sparc")
		echo "Symlinking needed libraries is not yet implemented for Sparc."
	;;
esac
