#! /bin/sh

# Copyright (c) 1994-2015 by Marc Feeley, All Rights Reserved.

herefromroot="prebuilt/macosx"
rootfromhere="../.."

PACKAGE_SHORTNAME="@PACKAGE_SHORTNAME@"
PACKAGE_NAME="@PACKAGE_NAME@"
PACKAGE_VERSION="@PACKAGE_VERSION@"
PACKAGE_STRING="@PACKAGE_STRING@"
PACKAGE_BUGREPORT="@PACKAGE_BUGREPORT@"
PACKAGE_TARNAME="@PACKAGE_TARNAME@"

# PackageMaker is now separate form Xcode and must be downloaded separately.
PKGM="/Applications/PackageMaker.app/Contents/MacOS/PackageMaker"

VARIANT="$1"
RUN_TESTS="$2"

SOURCE_DIR="$INSTALL_PREFIX"
INSTALL_DIR="$INSTALL_PREFIX"
BIN_DIR="/usr/local/bin"
LIB_DIR="/usr/local/lib"
INCLUDE_DIR="/usr/local/include"
TITLE="$PACKAGE_STRING ($VARIANT variant)"
VERSION="`echo $PACKAGE_VERSION | sed -e s/^v//g`"
PKG_FILE="`pwd`/$PACKAGE_TARNAME-macosx-$VARIANT.pkg"
DMG_FILE="`pwd`/$PACKAGE_TARNAME-macosx-$VARIANT.dmg"
BACKGROUND_FILE="`pwd`/gambit.tif"
PMPROJ_FILE="`pwd`/gambit.pmproj"
WELCOME_FILE="`pwd`/welcome.html"
README_FILE="`pwd`/readme.html"
CONCLUSION_FILE="`pwd`/conclusion.html"
EXTRAS_DIR="`pwd`/extras"
DESCRIPTION="This is the $TITLE installer"
PROGRAMS="gsc gsc-script gsi gsi-script scheme-ieee-1178-1990 scheme-r4rs scheme-r5rs scheme-srfi-0 six six-script gambcomp-C.bat gambdoc.bat uninstall-gambit"
LIBRARIES="libgambit@LIB_EXTENSION@ libgambitgsi@LIB_EXTENSION@ libgambitgsc@LIB_EXTENSION@"
INCLUDES="gambit.h"

instantiate_file() # input, output
{
  sed -e "s|@BIN_DIR@|$BIN_DIR|g" -e "s|@LIB_DIR@|$LIB_DIR|g" -e "s|@INCLUDE_DIR@|$INCLUDE_DIR|g" -e "s|@SOURCE_DIR@|$SOURCE_DIR|g" -e "s|@INSTALL_DIR@|$INSTALL_DIR|g" -e "s|@VERSION@|$VERSION|g" -e "s|@PACKAGE_VERSION@|$PACKAGE_VERSION|g" -e "s|@TITLE@|$TITLE|g" -e "s|@DESCRIPTION@|$DESCRIPTION|g" -e "s|@PROGRAMS@|$PROGRAMS|g" -e "s|@LIBRARIES@|$LIBRARIES|g" -e "s|@INCLUDES@|$INCLUDES|g" -e "s|@BACKGROUND_FILE@|$BACKGROUND_FILE|g" -e "s|@WELCOME_FILE@|$WELCOME_FILE|g" -e "s|@README_FILE@|$README_FILE|g" -e "s|@CONCLUSION_FILE@|$CONCLUSION_FILE|g" -e "s|@EXTRAS_DIR@|$EXTRAS_DIR|g" -e "s|@NAME@|$PACKAGE_NAME|g" -e "s|@VARIANT@|$VARIANT|g" "$1" > "$2"
}

cleanup()
{
  rm -rf "$PMPROJ_FILE" "$WELCOME_FILE" "$README_FILE" "$CONCLUSION_FILE" "$PKG_FILE" "$SOURCE_DIR" "$EXTRAS_DIR"
}

install_gambit()
{
  cd "$rootfromhere"
  make clean
  make -j 4
  make install

  if [ "$RUN_TESTS" == "0" ] ; then
    echo "************ TESTS SKIPPED ($VARIANT variant)"
  else

    TESTS_SUCCESS=0

    make check > build-phase2-tests.out

    cat build-phase2-tests.out

    if fgrep "============ ALL TESTS SUCCESSFUL" build-phase2-tests.out > /dev/null; then
      TESTS_SUCCESS=1
    fi

    rm -f build-phase2-tests.out

    if [ "$TESTS_SUCCESS" == "1" ] ; then
      echo "************ TESTS SUCCESSFUL ($VARIANT variant)"
    else
      echo "************ TESTS FAILED ($VARIANT variant)"
      cleanup
      exit 1
    fi

  fi

  cd "$herefromroot"
}

uninstall_gambit()
{
  "$SOURCE_DIR/$PACKAGE_VERSION/bin/uninstall-gambit"
}

create_uninstaller()
{
  rm -f "$SOURCE_DIR/$PACKAGE_VERSION/bin/uninstall-gambit"

  instantiate_file "uninstall-gambit.in" "$SOURCE_DIR/$PACKAGE_VERSION/bin/uninstall-gambit"

  chmod 755 "$SOURCE_DIR/$PACKAGE_VERSION/bin/uninstall-gambit"
}

create_extras()
{
  mkdir "$EXTRAS_DIR"

  touch "$EXTRAS_DIR/InstallationCheck.strings"

  i=0

  for f in $PROGRAMS ; do

    echo "\"`expr $i + 16`\" = \"The installation process is being aborted because the file $BIN_DIR/$f exists and it was not created by the Gambit installer.  A possible solution is to remove this file manually and restart the installer.\";" >> "$EXTRAS_DIR/InstallationCheck.strings"

    i=`expr $i + 1`

  done

  for f in $LIBRARIES ; do

    echo "\"`expr $i + 16`\" = \"The installation process is being aborted because the file $LIB_DIR/$f exists and it was not created by the Gambit installer.  A possible solution is to remove this file manually and restart the installer.\";" >> "$EXTRAS_DIR/InstallationCheck.strings"

    i=`expr $i + 1`

  done

  for f in $INCLUDES ; do

    echo "\"`expr $i + 16`\" = \"The installation process is being aborted because the file $INCLUDE_DIR/$f exists and it was not created by the Gambit installer.  A possible solution is to remove this file manually and restart the installer.\";" >> "$EXTRAS_DIR/InstallationCheck.strings"

    i=`expr $i + 1`

  done

  for file in InstallationCheck postflight ; do
    instantiate_file "$file.in" "$EXTRAS_DIR/$file"
    chmod 755 "$EXTRAS_DIR/$file"
  done
}

create_pmproj()
{
  for file in "$PMPROJ_FILE" "$WELCOME_FILE" "$README_FILE" "$CONCLUSION_FILE" ; do
    instantiate_file "$file.in" "$file"
  done

  plutil -convert binary1 "$PMPROJ_FILE"
}

create_pkg()
{
  $PKGM -build -proj "$PMPROJ_FILE" -p "$PKG_FILE"
}

create_dmg()
{
  hdiutil create -srcfolder "$PKG_FILE" -volname "$PACKAGE_STRING" -fs HFS+ "$DMG_FILE"
}

cd "`dirname $0`"

cleanup
rm -f "$DMG_FILE"
install_gambit
create_uninstaller
create_extras
create_pmproj
create_pkg
create_dmg
mv "$DMG_FILE" "$rootfromhere"
cleanup
