:
# make scribus 1.5

#	Remove extra environment symbols

cpag_def_opts="$CPAG_DEF_OPTS"

if [ -f "$HOME/bin/unsetall.sh" ] ; then . $HOME/bin/unsetall.sh ; fi

patchonly=no
patchdry=no
patchopt=
dopatch=yes
dorm=no
doinstall=def
dodebug=yes
cmakeopt=
version=15
dojpatch=yes
dowpatch=yes
jobs=

case ":${cpag_def_opts}:" in
:tmp:) binprefix=tmp ;;
*) binprefix=local ;;
esac

while [ ! -z "$1" ]
do
	case "$1" in
	-rm)		dorm=yes ;;				# remove the build area first
	-patchonly)	patchonly=yes ;;			# stop after applying patches
	-patchdry)	patchdry=yes ; patchonly=yes ;;		# test a dry run with patches and stop
	-patch)		dopatch=yes ;;				# apply patches
	-patchj)	dojpatch=yes ; dowpatch=no ;;		# Juraj patches only
	-patchw)	dowpatch=yes ; dojpatch=no ;;		# WB patches only
	-nopatch)	dopatch=no ;;				# do not apply patches
	-noi|-noinstall) doinstall=no ;;			# do not install
	-install)	doinstall=yes ;;			# install
	-g|-debug)	dodebug=yes ;;				# build with debug
	-nodebug)	dodebug=no ;;				# build without debug
	-localbin)	binprefix=local ;;			# install to the default, usually /usr/local/bin
	-tmpbin)	binprefix=tmp ;;			# install inside /tmp for testing on systems with a large RAM disk
	-gnubin)	binprefix=gnu ;;			# install inside /u for multiple versions of Scribus on the same server
	-j)		jobs="$2" ; shift ;;			# set the number of jobs at once
	-j0|-j1)	jobs=1 ;;				# 1 job
	-j2)		jobs=2 ;;				# 2 jobs
	-14)		version=14 ;;				# build 1.4
	-15)		version=15 ;;				# build 1.5
	*)		echo "$0: Unknown option $1" ; exit ;;
	esac
	shift
done

if [ -z "$jobs" ]
then
	jobs=4
fi

if [ "$doinstall" = def ]
then
	if [ "$version" = 14 -a "$binprefix" = local ] ; then doinstall=no ; else doinstall=yes ; fi
fi

if [ "$patchdry" = yes ]
then
	patchopt="$patchopt --dry"
fi

set +x

umask 0

echo "$0: Scribus $version build started at `date`"

name=scribus$version
main=/u3/gnu
dest=/tmp

mainname="$main/$name"
destname="$dest/$name"
if [ -d "$destname/Scribus" ] ; then destname="$destname/Scribus" ; fi

patchdir="/u/william/test/scribus/patches-$version"

if [ -d /usr/local/lib/pkgconfig -a -d /usr/lib64/qt5/lib/pkgconfig ]
then
	# qt5 and dependencies built outside base distribution
	export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/lib64/qt5/lib/pkgconfig
elif [ -d /usr/lib64/pkgconfig ]
then
	# qt5 included in base distribution
	export PKG_CONFIG_PATH=/usr/lib64/pkgconfig/
else
	echo "$0: Warning: not updating PKG_CONFIG_PATH, did not find pkconfig library"
fi

if [ -d /usr/lib64/ccache ]
then
	case ":${PATH}:" in
	*:/usr/lib64/ccache:*) echo "PATH already has ccache" ;;
	*) PATH="/usr/lib64/ccache:$PATH" ;;
	esac
fi

if [ -d /usr/lib64/qt5/bin ]
then
	case ":${PATH}:" in
	*:/usr/lib64/qt5/bin:*) echo "PATH already has qt5 bin" ;;
	*) PATH="/usr/lib64/qt5/bin:$PATH" ;;
	esac
elif [ -d "/usr/bin/qt5" ]
then
	case ":${PATH}:" in
	*:/usr/qt5/bin:*) echo "PATH already has qt5 bin" ;;
	*) PATH="/usr/qt5/bin:$PATH" ;;
	esac
fi

if [ -d /usr/lib/qtchooser ]
then
	case ":${PATH}:" in
	*:/usr/lib/qtchooser:*) echo "PATH already has qtchooser" ;;
	*) PATH="/usr/lib/qtchooser:$PATH" ;;
	esac
fi

if [ -d /usr/local/bin ]
then
	case ":${PATH}:" in
	*:/usr/local/bin:*) echo "PATH already has local bin" ;;
	*) PATH="/usr/local/bin:$PATH" ;;
	esac
fi

export PATH

export QT_SELECT=qt5

case "$binprefix" in
tmp) cmakeopt="$cmakeopt -DCMAKE_INSTALL_PREFIX:PATH=/tmp/scribus${version}bin" ;;
gnu) cmakeopt="$cmakeopt -DCMAKE_INSTALL_PREFIX:PATH=/u/gnu/scribus${version}bin" ;;
local) ;;
*) echo "$0: Unknown bin prefix $binprefix" ;;
esac

if [ -d /usr/lib64/qt5 ]
then
	cmakeopt="$cmakeopt -DQT_PREFIX=/usr/lib64/qt5"
else
	echo "Not setting cmake QT_PREFIX, did not find /usr/lib64/qt5"
fi

if [ "$dodebug" = yes ]
then
	cmakeopt="$cmakeopt -DWANT_DEBUG=1"
fi

if [ -x /usr/bin/gm -o -x /usr/local/bin/gm ]
then
	cmakeopt="$cmakeopt -DWANT_GRAPHICSMAGICK=1"
else
	echo "Not setting cmake WANT_GRAPHICSMAGICK, did not find gm"
fi

echo "PKG_CONFIG_PATH is $PKG_CONFIG_PATH"
echo "PATH            is $PATH"
echo "QT_SELECT       is $QT_SELECT"

if [ "$dorm" = yes ]
then
	if [ -d "$destname" ]
	then
		echo "Removing $destname ..."
		case "$destname" in
		/tmp/*) ;;
		*) echo "$0: Error: destname $destname does not have /tmp/" ; exit ;;
		esac
		sudo rm -rf "$destname"
	else
		echo "$0: Warning: destname $destname does not exist, ignoring remove option"
	fi
fi

if [ -d "$destname" ]
then
	echo "$0: $destname exists"
else
	echo "$0: Copying $mainname to $destname"

	if [ ! -d "$mainname" ]
	then
		echo "$0: Error: $mainname not found"
		exit
	fi

	cd "$mainname"

	if [ ! -d .svn ]
	then
		echo "$0: Error: $mainname is not an svn repository"
		exit
	fi

	svn update

	cp -pr "$mainname" "$destname"

	cd "$destname"

	check=`pwd`

	if [ "$check" != "$destname" ]
	then
		echo "$0: Error: could not move to $destname"
		exit
	fi

	if [ ! -d .svn ]
	then
		echo "$0: Error: $destname is not an svn repository"
		exit
	fi

	echo "Checking for svn updates ..."

	svn status --no-ignore | egrep '^\?|^I' | sed 's/^. *//' | xargs -Ixx rm -rf xx

	if [ "$dopatch" = yes -a "$dojpatch" = yes ]
	then
		# Juraj patches to apply without a backup.

		echo "Applying Juraj patches ..."

		for patchfile in $patchdir/[0-9]*-*.pat{,ch}
		do
			if [ -f "$patchfile" ]
			then
				echo "Applying Juraj patch $patchfile ..."

				if ! patch -p1 $patchopt < "$patchfile"
				then
					echo "$0: Error: patch file $patchfile failed."
					exit
				fi
			fi
		done
	else
		echo "Not applying Juraj patches"
	fi

	if [ "$dopatch" = yes -a "$dowpatch" = yes ]
	then
		# William patches to apply with a backup.
		#   There should be at most one of these to make it possible to reconstruct.

		echo "Applying WB patches ..."

		for patchfile in $patchdir/scribus*[0-9]*-*.pat{,ch}
		do
			if [ -f "$patchfile" ]
			then
				echo "Applying WB patch $patchfile ..."

				if ! patch -p1 --backup --suffix=- $patchopt < "$patchfile"
				then
					echo "$0: Error: patch file $patchfile failed."
					exit
				fi
			fi
		done
	else
		echo "Not applying WB patches"
	fi

	if [ "$patchonly" = yes ]
	then
		exit
	fi
fi

if [ "$patchonly" = yes ]
then
	exit
fi

cd "$destname"

check=`pwd`

if [ "$check" != "$destname" ]
then
	echo "$0: Error: could not move to $destname"
	exit
fi

if [ ! -f Makefile ]
then
	echo "Running cmake$cmakeopt ..."

	do1t cmake $cmakeopt .
else
	echo "$0: Makefile exists, not rerunning cmake"
fi

if [ ! -f Makefile ]
then
	echo "$0: Error: no Makefile in $destname"
	exit
fi

echo "Running make ..."

if [ "$binprefix" = tmp -a "$doinstall" = yes ]
then
	nice sudo make -j "$jobs" -k install
else
	if nice make -j "$jobs" -k
	then
		if [ "$doinstall" = yes ]
		then
			echo "Installing ..."

			sudo make -j "$jobs" install
		else
			echo "Not installing"
		fi
	fi
fi

echo "$0: Scribus $version build finished at `date`"

