#!/usr/bin/env zsh

# Build a Windows-native version of XEmacs.
# Usage: MakeNativeXEmacs [-h|--help] [--with-mule] [--without-mule] [--with-debug] [--without-debug] [--package-root=path] [--prefix=path] [--install] dirname
#
# Assumptions:
#   1) Cygwin is installed on the build machine, including makeinfo, perl,
#      cvs, tar and probably other stuff, too.
#   2) Optional library sources are in /usr/local/src (see LIB_DIR).
#   3) zlib is in /usr/local/src/zlib (see ZLIB_VER).
#   4) tiff is installed in /usr/local/src/tiff-3.7.3 (see TIFF_VER).
#   5) Visual Studio is installed (see the sourced script vcsetup).

# Set up a vanilla zsh environment
emulate -LR zsh
progname=${0:t}
#0=$progname                             # For error messages from builtins

function usage {
  print -u2 "\
usage: $progname [-h|--help] [--with-mule] [--without-mule] [--with-debug] [--without-debug] [--package-root dir] [--prefix dir] [--install] [--intel] [--sdk] dirname
       -h|--help            print this help message and exit
       --with-mule          turn on MULE support
       --without-mule       turn off MULE support
       --with-debug         turn on debugging
       --without-debug      turn off debugging
       --package-root=dir   specify directory for root of installed packages
       --prefix=dir         specify installation directory
       --install            install the executable
       --intel              use the intel compiler
       --sdk                build with the Microsoft SDK
       dirname              path to the source directory"
}

# Things to customize
: ${GIFLIB_VER:=giflib-4.1.6}
: ${LIB_DIR:=/usr/local/src}
: ${PNG_VER:=libpng-1.2.46}
: ${TIFF_VER:=tiff-3.9.5}
: ${ZLIB_VER:=zlib}

zparseopts -D h=help -help=help -with-mule=with_mule -without-mule=without_mule -with-debug=with_debug -without-debug=without_debug -package-root:=package_root -prefix:=prefix -install=install -sdk=with_sdk -intel=with_intel

if [[ -n $help ]]; then
  usage
  exit 0
fi

# Set up for the Visual Studio compiler
unset CC                # Don't allow gcc settings to affect us
[[ -n $with_sdk ]] && with_sdk=SDK
if [[ -n $with_intel ]]; then
  export CC=icl
  with_intel=intel
fi
. vcsetup $with_sdk $with_intel || exit $?

# Strip out option strings and '='
install_dir=${prefix[2]#=}
package_dir=${package_root[2]#=}

# 3 options for debug and mule: with, without or no change from default
debug=
[[ -n $with_debug ]] && debug=1
[[ -n $without_debug ]] && debug=0
mule=
[[ -n $with_mule ]] && mule=1
[[ -n $without_mule ]] && mule=0

case $1 in
(*21.4*|*21.5*)
  ;;
(*)
  print -u2 "$progname: Only XEmacs 21.4 and XEmacs 21.5 are currently supported."
  usage
  exit 1
  ;;
esac

# Configure the sources
srcdir=$1
if [[ ! -e $srcdir ]]; then
  print -u2 $srcdir does not exist.
  exit 1
fi

cd $srcdir/nt
unset HOME              # Don't pick up my emacs customizations
# Configure for optional packages
perl -pi -e "s@^OPTIONAL_LIBRARY_DIR=.*@OPTIONAL_LIBRARY_DIR=$(cygpath --mixed $LIB_DIR)@;
             s@^PNG_DIR=.*@PNG_DIR=\\\$(OPTIONAL_LIBRARY_DIR)/$PNG_VER@;
             s@^GIF_DIR=.*@GIF_DIR=\\\$(OPTIONAL_LIBRARY_DIR)/$GIFLIB_VER@;
             s@^ZLIB_DIR=.*@ZLIB_DIR=\\\$(OPTIONAL_LIBRARY_DIR)/$ZLIB_VER@;
             s@^TIFF_DIR=.*@TIFF_DIR=\\\$(OPTIONAL_LIBRARY_DIR)/$TIFF_VER@;
             s@^HAVE_XFACE=.*@HAVE_XFACE=0@;
             s@^USE_KKCC=.*@USE_KKCC=0@;
             s@^MAKEINFO=.*@MAKEINFO=${(q)$(cygpath --windows =makeinfo.exe)}@" < config.inc.samp >config.inc

# Change installation directory if requested
if [[ -n $install_dir ]]; then
  # Use the source directory to generate the installation directory
  # name; e.g. XEmacs-21.4.22 or XEmacs-21.4-2009-01-05.
  version_string=XEmacs${$(basename $srcdir)#xemacs}
  perl -pi -e "s@^INSTALL_DIR=.*@INSTALL_DIR=$install_dir/$version_string@" config.inc
fi

# Configure debugging if requested
[[ -n $debug ]] && perl -pi -e "s@^DEBUG_XEMACS=.*@DEBUG_XEMACS=$debug@" config.inc

# Don't require the C runtime debug library
# This keeps the setup kits from requiring msvcrtd.dll
perl -pi -e 's@.*BUILD_FOR_SETUP_KIT.*@BUILD_FOR_SETUP_KIT=1@' config.inc

# Configure MULE if requested (only for 21.5)
[[ -n $mule ]] &&  perl -pi -e "s@^MULE=.*@MULE=$mule@" config.inc

# Use the Intel C Compiler if requested
[[ -n $with_intel ]] && perl -pi -e "s@^USE_INTEL_COMPILER=.*@USE_INTEL_COMPILER=1@" config.inc

# Configure package path if requested
[[ -n $package_dir ]] &&  perl -pi -e "s@^#PACKAGE_PREFIX=.*@PACKAGE_PREFIX=$package_dir@" config.inc

# Build and install XEmacs
nmake -f xemacs.mak || exit $?
nmake -f xemacs.mak check >& check.out || exit $?
if [[ -n $install ]]; then
  nmake -f xemacs.mak install || exit $?
fi

exit 0

# Local Variables:
# mode: ksh
# sh-indentation: 2
# indent-tabs-mode: nil
# End:
