#!/bin/sh
#
# $Id: build_gerbv,v 1.7 2008/01/31 23:39:34 danmc Exp $
#

# error out on failed commands whose return code wasn't explicitly
# checked
set -e

usage() {
cat << EOF

$0 [options]

Builds a non-cygwin version of gerbv and create a standalone
windows installer.

Supported options:

  --debug         - Omits the compiler flag which prevents
                    a command window from being opened.  This
                    is useful when trying to use debug printf's

  --help          - Show this message and exit.

  --force-autogen - Force running ./autogen.sh.  Normally this is
                    only done if the configure script is not present.

  --skip-build    - Skip the "make" step of the process.

  --skip-clean    - Skip the "make clean" step of the process.

  --skip-config   - Skip the "./configure" step of the process.

  --skip-install  - Skip the "make install" step of the process.

For the $0 script to work, you must have the gtk_win32 files
as well as gdlib installed on your system in very specific
locations.  Edit $0 to change these.  While you are at it, feel
free to provide a patch to improve the documentation about 
those libraries.

EOF
}

debug=no
do_autogen=no
do_config=yes
do_build=yes
do_clean=yes
do_install=yes
config_args=""
while test $# -ne 0 ; do
	case $1 in
		--debug)
			debug=yes
			shift
			;;

		--help)
			usage
			exit 0
			;;

		--force-autogen)
			do_autogen=yes
			shift
			;;

		--skip-build)
			do_build=no
			shift
			;;

		--skip-clean)
			do_clean=no
			shift
			;;

		--skip-config)
			do_config=no
			shift
			;;

		--skip-install)
			do_install=no
			shift
			;;

		-*)
			echo "ERROR:  Unknown option $1"
			usage
			exit 1
			;;

		*)
			break
			;;
	esac
done

if test ! -f win32/build_gerbv ; then
	echo "$0:  ERROR.  This script must be run from the top level of the gerbv source tree."
	exit 1
fi

# Run this under cygwin to build gerbv and create a windows installer for
# it.  Thanks to Bob Paddock for pointing me to NSIS and answering some 
# beginner windows questions.

# where gtk_win32 is installed
gtk_win32=c:\\cygwin\\home\\${USER}\\gtk_win32

# where only the runtime components are installed
gtk_win32_runtime=c:\\\\cygwin\\\\home\\\\${USER}\\\\gtk_win32_runtime

# gerbv version

gerbv_version=`awk '/AC_INIT/ {gsub(/.*,[ \t]*\[/, ""); gsub(/\]\).*/, ""); print}' configure.ac`
echo "gerbv_version=${gerbv_version}"

# location of the NSIS makensis executible (see http://nsis.sourceforge.net)
makensis="/cygdrive/c/Program Files/NSIS/makensis.exe"


# ########################################################################
#
# The rest should be ok without editing
#
# ########################################################################


if test ! -f configure -o $do_autogen = yes ; then
	echo "Bootstrapping autotools"
	ACLOCAL_FLAGS="-I ${gtk_win32}\\share\\aclocal" ./autogen.sh
fi

# source directory
srcdir=`pwd.exe`/win32
top_srcdir=${srcdir}/..

src_dir=c:\\\\cygwin`echo ${srcdir} | sed 's;/;\\\\\\\\;g'`
top_src_dir=c:\\\\cygwin`echo ${top_srcdir} | sed 's;/;\\\\\\\\;g'`


# where to install gerbv
gerbv_inst=`pwd`/gerbv_inst

# DOS version
gerbv_inst_dir=c:\\\\cygwin`echo ${gerbv_inst} | sed 's;/;\\\\\\\\;g'`

PKG_CONFIG_PATH=${gtk_win32}\\lib\\pkgconfig
export PKG_CONFIG_PATH

PATH=${gtk_win32}\\bin:${gd_win32}:${PATH}
export PATH

echo "Showing packages known to pkg-config:"
pkg-config --list-all


# do not try to use libpng-config, it seems broken on win32
LIBPNG_CONFIG=/usr/bin/true
export LIBPNG_CONFIG
LIBPNG_CFLAGS="-I${HOME}/gtk_win32/include"
LIBPNG_LDFLAGS="-L${HOME}/gtk_win32/lib"
LIBPNG_LIBS="-lpng"

# add the gcc options to produce a native windows binary that
# does not need cygwin to run
if test "x${debug}" = "xno" ; then
	EXTRA_FLAGS="-mwindows"
fi

CYGWIN_CFLAGS="-mms-bitfields -mno-cygwin ${EXTRA_FLAGS}"
export CYGWIN_CFLAGS

CYGWIN_CPPFLAGS="-mms-bitfields -mno-cygwin ${EXTRA_FLAGS}"
export CYGWIN_CPPFLAGS

# setting WIN32=yes will make sure that the desktop icon
# gets compiled in
if test "$do_config" = "yes" ; then
rm -fr src/.deps
echo "Configuring for cygwin"
( ( ( env WIN32=yes \
	./configure \
	--prefix=${gerbv_inst} \
	--disable-dependency-tracking \
	--disable-nls \
	--disable-update-desktop-database \
	--disable-update-mime-database \
	${config_args} \
	CFLAGS="${LIBPNG_CFLAGS}" \
	LDFLAGS="${LIBPNG_LDFLAGS}" \
	LIBS="${LIBPNG_LIBS}" \
	WIN32=yes \
	2>&1 ; echo $? >&4 ) | tee c.log 1>&3) 4>&1 | (read a ; exit $a)) 3>&1

if test $? -ne 0 ; then
	echo "**** ERROR **** Configure failed. See log in c.log"
	exit 1
fi

# If the win32 pkg-config is used, then you end up with spurious CR's
# in the generated Makefile's and we need to get rid of them.

echo "Removing spurious carriage returns in the Makefiles..."
for f in `find . -name Makefile -print ; do
	mv $f $f.bak
	cat $f.bak | tr '\r' ' ' > $f
	rm $f.bak
done

fi

if test "$do_clean" = "yes" ; then
echo "Cleaning"
( ( ( make clean 2>&1 ; echo $? >&4) | tee clean.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
if test $? -ne 0 ; then
	echo "**** ERROR **** Clean failed. See log in clean.log"
	exit 1
fi
fi

if test "$do_build" = "yes" ; then
echo "Building for cygwin"
( ( ( make 2>&1 ; echo $? >&4) | tee m.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
if test $? -ne 0 ; then
	echo "**** ERROR **** Build failed. See log in m.log"
	exit 1
fi
fi

if test "$do_install" = "yes" ; then
echo "Installing for cygwin"
# first clean out the installation directory to make sure
# we don't have old junk lying around.
if test -d ${gerbv_inst} ; then
	rm -fr ${gerbv_inst}
fi
( ( ( make install 2>&1 ; echo $? >&4) | tee -a m.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
if test $? -ne 0 ; then
	echo "**** ERROR **** Build failed. See log in m.log"
	exit 1
fi
fi

echo "Creating NSIS script"
echo "srcdir = ${srcdir}"
echo "src_dir = ${src_dir}"
echo "top_srcdir = ${top_srcdir}"
echo "top_src_dir = ${top_src_dir}"

sed \
	-e "s;@gerbv_version@;${gerbv_version};g" \
	-e "s;@gerbv_prefix@;${gerbv_inst_dir};g" \
	-e "s;@gerbv_srcdir@;${top_src_dir};g" \
	-e "s;@gtk_win32_runtime@;${gtk_win32_runtime};g" \
	${srcdir}/gerbv.nsi.in > ${srcdir}/gerbv.nsi

echo "Creating windows installer"
"${makensis}" ${src_dir}/gerbv.nsi


echo "Windows installer left in ${srcdir}:"
ls -l ${srcdir}/*.exe


