#!/bin/sh
#
# $Id: build_wcalc,v 1.3 2007/11/26 22:14:34 dan Exp $
#

# Copyright (c) 2006, 2007 Dan McMahill
# All rights reserved.
#
# This code is derived from software written by Dan McMahill
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by Dan McMahill
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.


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


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

  --skip-build    - Skip the "make" 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
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_config=yes
do_build=yes
do_install=yes
while test $# -ne 0 ; do
	case $1 in
		--debug)
			debug=yes
			shift
			;;

		--help)
			usage
			exit 0
			;;

		--skip-build)
			do_build=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


# 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

# wcalc version
wcalc_version=`awk '/AM_INIT_AUTOMAKE/ {gsub(/.*,[ \t]*/, ""); gsub(/\).*/, ""); print}' configure.ac `
echo "wcalc_version=${wcalc_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
#
# ########################################################################


# 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 wcalc
wcalc_inst=`pwd`/wcalc_inst

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

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

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

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

if test "$do_config" = "yes" ; then
echo "Configuring for cygwin"
./configure \
	--prefix=${wcalc_inst} \
	--enable-gtk2 \
	--disable-cgi \
	--disable-htdocs \
	--disable-nls \
	--disable-stdio \
	--without-matlab \
	--without-octave \
	--without-scilab \
	2>&1 | tee c.log

fi

if test "$do_build" = "yes" ; then
echo "Building for cygwin"
make 2>&1 | tee m.log

echo "Installing for cygwin"
make install 2>&1 | tee -a m.log

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;@wcalc_version@;${wcalc_version};g" \
	-e "s;@wcalc_prefix@;${wcalc_inst_dir};g" \
	-e "s;@wcalc_srcdir@;${top_src_dir};g" \
	-e "s;@gtk_win32_runtime@;${gtk_win32_runtime};g" \
	${srcdir}/wcalc.nsi.in > ${srcdir}/wcalc.nsi

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

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


