#!/bin/bash
# --- 
# --- install_win32.sh
# --- 
# --- (c) 2006-2008 Bastian Spiegel <bs@tkscript.de>
# ---      - distributed under terms of the GNU general public license (GPL).
# --- 
# --- 
# --- This is a simple build script for 
# ---         - tks
# ---         - tksdl
# ---         - tkopengl
# ---         - tkmath
# ---         - tkfreetype2
# ---         - tkui
# ---         - tkmidi
# ---         - tkcg
# ---         - tkchipmunk
# ---         - tkfreeglut
# ---         - tkoldmath
# ---         - eqxmms
# ---
# --- *** Please install MSYS and run /c/msys/1.0/bin/sh.exe ! ***
# ---

# ---
# --- Please adjust the following variables to match your local installation paths
# ---

#
# Set this to the regular Win32 TKS installation directory
#
#export TKS_PATH=/cygdrive/c/Programme/tks
export TKS_PATH="/c/Program Files/tks"


#
# Prefix to use when installing tks in the Cygwin environment
# The tks.exe will be installed to $TKS_PREFIX/tks/ and the plugins 
# will be placed in $TKS_PREFIX/tks/plugins/
#
export TKS_PREFIX=/OBSOLETE_VAR_DO_NOT_USE/usr


#
# Set this to the Visual Studio installation directory.
# Includes/libraries will be searched in $VCTK/include and $VCTK/lib
#
export VCTK="c:/Program Files/Microsoft Visual Studio 9.0/VC"
#export VCTK=c:/Programme/Microsoft Visual C++ Toolkit 2003
#export VCTK="c:/Programme/Microsoft Visual Studio 8/VC"
#export VCTK="f:/fli/tools/dev/MSVCTK2003"


#
# Set this to the OpenWatcom installation directory. 
# This is only required if you select the watcom compiler using the
#  -a watcom  option.
#
export WATCOM=c:/WATCOM
#export WATCOM=f:/fli/tools/dev/WATCOM
#export WATCOM=d:/watcom
#WATCOM=c:/bsp/bin/WATCOM


#
# Set this to the Intel C++ installation directory resp. license file.
# This is only required if you select the intel compiler using the
#  -a intel  option.
#
#export INTEL=c:/Programme/Intel/Compiler/C++/9.0/IA32
#export INTEL_LICENSE_FILE=c:/bsp/evaluation_cpp_w_V887-211907831.lic
export INTEL=c:/Programme/Intel/Compiler/C++/10.1.011/IA32
export INTEL_LICENSE_FILE=c:/EVAL_W_CMP_CPP_V94K-M6S75J95.lic


#export FREETYPESRC=c:/bsp/sources/freetype-2.1.9
#export FREETYPESRC=f:/sources/freetype-2.1.10
#export FREETYPESRC_GNU=/cygdrive/f/sources/freetype-2.1.10  # newer versions of make do not allow : in source file names :(
# Since make does not allow windows style path names anymore I had to copy the freetype source to the tkfreetype2 plugins directory!


#
# Set this to the platform SDK installation directory
#
#export W32API=d:/Programme/Microsoft Platform SDK
#export W32API=f:/fli/tools/dev/MSVCTK2003/w32api
#export W32API='c:/Programme/Microsoft Platform SDK'
export W32API='c:/Program Files/Microsoft SDKs/Windows/v6.0a'


#
# Set this to the DirectX SDK installation directory
#
#export DXSDK_LIB=f:/fli/tools/dev/MSVCTK2003/w32api/lib
#export DXSDK_LIB=c:/Programme/Microsoft DirectX SDK (December 2005)/lib/x86
#export DXSDK_LIB='c:/Programme/Microsoft DirectX SDK (August 2006)/Lib/x86'
export DXSDK_LIB='c:/Program Files/Microsoft DirectX SDK (August 2008)/Lib/x86'

#export DXSDK_INC=f:/fli/tools/dev/MSVCTK2003/w32api/include
#export DXSDK_INC=c:/Programme/Microsoft DirectX SDK (December 2005)/include
#export DXSDK_INC='c:/Programme/Microsoft DirectX SDK (August 2006)/Include'
export DXSDK_INC='c:/Program Files/Microsoft DirectX SDK (August 2008)/Include'



# ---
# --- Must be one of
# ---     makefile.cygwin  (tested with gcc 3.4.4)
# ---     makefile.msvc    (tested with VC6/VCTK2003/VS8)
# ---     makefile.wintel  (tested with Intel C++ 9.0, 10.1.011)
# ---     makefile.watcom  (tested OpenWatcom 1.4)
# ---
MAKEFILE=makefile.msvc

# ---
# --- Number of parallel targets to make
# ---
NUMJOBS=4

# ---
# --- Must be one of
# ---     <unset>  - use $DEFAULT_MAKEFILE
# ---     gcc      - use makefile.cygwin
# ---     msvc     - use makefile.msvc
# ---     intel    - use makefile.wintel
# ---     watcom   - use makefile.wincom
# ---
ARCH=msvc

# ---
# --- Nothing to see below this line :)
# ---

determineMakefileByArch() {
  case "$ARCH" in
    "gcc")
      MAKEFILE=makefile.cygwin
      ;;
    "msvc")
      MAKEFILE=makefile.msvc
      ;;
    "intel")
      MAKEFILE=makefile.wintel
      ;;
    "watcom")
      MAKEFILE=makefile.wincom
      ;;
  esac
}


BUILD_CALLED=0

build() {
    _target=$1
    BUILD_CALLED=1
    cd tks-source     && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tksdl       && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkopengl    && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkmath      && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkfreetype2 && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkui        && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkmidi      && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkcg        && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkchipmunk  && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkfreeglut  && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd tkoldmath   && make -j $NUMJOBS -f $MAKEFILE $_target && cd .. \
    && cd eqxmms/src  && make -j $NUMJOBS -f $MAKEFILE $_target && cd ../..
}

printUsage() {
  echo "Usage:"
  echo "        install_win32.sh [-a <arch>]|[-f <makefile.arch>] [shared] [install] [clean]"
  echo "           shared  : build tks/tksdl/tkopengl/tkfreetype2/tkui/eqxmms targets"
  echo "           install : install targets"
  echo "           clean   : remove object files, targets (.exe/.dll) and backup files"
  echo ""
  echo "        <makefile.arch> must be one of:"
  echo "             makefile.cygwin [default]"
  echo "             makefile.msvc   "
  echo "             makefile.wintel "
  echo "             makefile.wincom "
  echo ""
  echo "        <arch> must be one of:"
  echo "             gcc [default]"
  echo "             msvc   "
  echo "             intel  "
  echo "             watcom "
  echo ""
  echo "Current path variables:"
  echo "  C++ compiler:"
  echo "                      VCTK => \"$VCTK\""
  echo "                    WATCOM => \"$WATCOM\""
  echo "                     INTEL => \"$INTEL\""
  echo "        INTEL_LICENSE_FILE => \"$INTEL_LICENSE_FILE\""
  echo ""
  echo "  General:"
  echo "               FREETYPESRC => \"$FREETYPESRC\""
  echo ""
  echo "  MSVC/INTEL specific:"
  echo "                    W32API => \"$W32API\""
  echo "                 DXSDK_INC => \"$DXSDK_INC\""
  echo "                 DXSDK_LIB => \"$DXSDK_LIB\""
  echo ""
  echo ""
  echo "  TKS installation dir/cygwin prefix:"
  echo "                  TKS_PATH => \"$TKS_PATH\""
  echo "                TKS_PREFIX => \"$TKS_PREFIX\""
  echo ""
  echo ""
  echo "Makefile: \"$MAKEFILE\"."
  echo ""
}

if [ "$#" -eq 0 ]; then
  printUsage
else
  while [ -n "$1" ]; do
    if [ "-f" = "$1" ]; then
      shift
      if [ -z "$1" ]; then
         printUsage;
      else
         MAKEFILE=$1
      fi
    elif [ "-a" = "$1" ]; then
      shift
      if [ -z "$1" ]; then
         printUsage;
      else
         ARCH=$1
         determineMakefileByArch
         echo "[...] Architecture set to \"$ARCH\". Now using Makefile \"$MAKEFILE\"."
      fi
    else
      echo "[...] make -f $MAKEFILE $1"
      build $1
    fi
    shift
  done
fi

if [ $BUILD_CALLED -eq 0 ]; then
  printUsage
fi

