#!/bin/sh
#
# $Id: build_pcb,v 1.15 2008/01/31 22:17:27 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 pcb 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

  --disable-doc   - Pass "--disable-doc" to the configure script.

  --enable-maintainer-mode
                  - Pass "--enable-maintainer-mode" to the
                    configure script.


  --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.

  --with-tex      - Set TEX=tex

  --with-etex     - set TEX=etex

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=""
tex_flag="TEX=tex"
while test $# -ne 0 ; do
	case $1 in
		--debug)
			debug=yes
			shift
			;;

               --disable-doc)
                       config_args="${config_args} --disable-doc"
                       shift
                       ;;

               --enable-maintainer-mode)
                       config_args="${config_args} --enable-maintainer-mode"
                       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
			;;

		--with-etex)
			tex_flag="TEX=etex"
			shift
			;;

		--with-tex)
			tex_flag="TEX=tex"
			shift
			;;

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

		*)
			break
			;;
	esac
done

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

# Run this under cygwin to build pcb 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


# where the gdwin32.zip archive has been extracted
# gdwin32.zip can be downloaded from http://www.boutell.com/gd/http/gdwin32.zip
# I have used 2.0.33
gd_win32=c:\\cygwin\\home\\${USER}\\gdwin32
gd_win32_runtime=c:\\\\cygwin\\\\home\\\\${USER}\\\\gdwin32


# pcb version

pcb_version=`awk '/AC_INIT/ {gsub(/.*,[ \t]*\[/, ""); gsub(/\]\).*/, ""); print}' configure.ac`
echo "pcb_version=${pcb_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 pcb
pcb_inst=`pwd`/pcb_inst

# DOS version
pcb_inst_dir=c:\\\\cygwin`echo ${pcb_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"

# do not try and build the tk based QFP footprint
# builder
WISH=/usr/bin/true
export WISH

# do not try and use the gdlib-config script to 
# configure gdlib
GDLIB_CONFIG=/usr/bin/true
export GDLIB_CONFIG
 
GDLIB_CFLAGS="-I${HOME}/gdwin32"
GDLIB_LDFLAGS="-L${HOME}/gdwin32"

# 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=${pcb_inst} \
       ac_cv_func_gdImageGif="yes" \
       ac_cv_func_gdImageJpeg="yes" \
       ac_cv_func_gdImagePng="yes" \
	--disable-dependency-tracking \
	--disable-nls \
	--disable-update-desktop-database \
	--disable-update-mime-database \
	${config_args} \
        CFLAGS="${GDLIB_CFLAGS}" \
        LDFLAGS="${GDLIB_LDFLAGS}" \
	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.

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 $tex_flag 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 ${pcb_inst} ; then
	rm -fr ${pcb_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;@pcb_version@;${pcb_version};g" \
	-e "s;@pcb_prefix@;${pcb_inst_dir};g" \
	-e "s;@pcb_srcdir@;${top_src_dir};g" \
	-e "s;@gtk_win32_runtime@;${gtk_win32_runtime};g" \
	-e "s;@gd_win32@;${gd_win32};g" \
	-e "s;@gd_win32_runtime@;${gd_win32_runtime};g" \
	${srcdir}/pcb.nsi.in > ${srcdir}/pcb.nsi

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


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


