#!/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] <21.4|21.5>
#
# 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] <21.4|21.5*>
       -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
       21.4                 build a 21.4 XEmacs kit from SCM
       21.5*                build some type of 21.5 XEmacs kit from SCM"
}

# Things to customize
: ${LIB_DIR:=/usr/local/src}
: ${ZLIB_VER:=zlib}
: ${TIFF_VER:=tiff-3.7.3}

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

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

# Make a local copy of the source tree
if ! srcdir=$(GetSource --src=$PWD xemacs-$XEMACS_VER) ; then
  print -u2 "$progname: could not get retrieve sources for xemacs-$XEMACS_VER."
  exit 2
fi

# Configure the sources
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@^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@^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
if [[ -n $package_dir ]] &&  perl -pi -e "s@^#PACKAGE_PREFIX=.*@PACKAGE_PREFIX=$package_dir@" config.inc

# Build and install XEmacs
nmake -f xemacs.mak
nmake -f xemacs.mak check >& check.out
[[ -n $install ]] && nmake -f xemacs.mak install

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