#!/bin/sh

### File: "build-gambit-iOS"

### Copyright (c) 2010-2012 by Marc Feeley, All Rights Reserved.

# The following definitions should be adjusted to your context.

# Which iOS kind and version are to be used.
ios_kind="iPhone"
#ios_version="5.0"
ios_version="4.3"

# Which ./configure options are to be used.
#config_options="--enable-single-host --enable-debug"
config_options="--enable-single-host"
#config_options=""
prefix_subdir="current"

# The following two settings are only relevant when this script is used
# outside of the Gambit distribution tree.  In this case the Gambit
# distribution must be downloaded.
gambit_dist_if_downloaded="gambc-v4_6_4-devel"
update_with_latest_changes_if_downloaded="yes"

select_macosx()
{
  gambit_dir="`pwd`/gambit-macosx"
  gambit_prefix="$gambit_dir/$prefix_subdir"

  echo "*** Selecting Mac OS X."

  config_options_extras=

  export CC="gcc"
  export CXX="g++"
  export CFLAGS="-I$gambit_prefix/include -L$gambit_prefix/lib"
  export CXXFLAGS="$CFLAGS"
  export LD="ld"
  export LDFLAGS=""
}

select_ios()
{
  kind="$1"
  platform_type="$2"
  sdk_version="$3"
  arch="$4"

  gambit_dir="`pwd`/gambit-iOS"
  #gambit_dir="`pwd`/gambit-$kind$platform_type"
  gambit_prefix="$gambit_dir/$prefix_subdir"

  ios_platform="$kind$platform_type.platform"
  ios_sdk="$kind$platform_type$sdk_version.sdk"

  platforms_dir="/Developer/Platforms"
  ios_platform_dir="$platforms_dir/$ios_platform"
  ios_sdks="$ios_platform_dir/Developer/SDKs"
  ios_sdk_dir="$ios_sdks/$ios_sdk"

  echo "*** Selecting platform \"$ios_platform\" and SDK \"$ios_sdk\" for \"$arch\"."

  if [ ! -d "$ios_platform_dir" ]; then
    echo "*** ERROR!  The platform \"$ios_platform\" is not available."
    echo "*** The available platforms are:"
    (cd "$platforms_dir" ; ls -d $kind* | sed -e "s/^/***   /g")
    echo "*** You need to edit the top of the build-gambit-iOS script"
    exit 1
  fi

  if [ ! -d "$ios_sdk_dir" ]; then
    echo "*** ERROR!  The SDK \"$ios_sdk\" is not available."
    echo "*** The available SDKs are:"
    (cd "$ios_sdks" ; ls | sed -e "s/^/***   /g")
    echo "*** You need to edit the top of the build-gambit-iOS script"
    exit 1
  fi

  case "$kind$platform_type" in

           iPhoneOS) config_options_extras=--host=arm-apple-darwin
                     ;;

    iPhoneSimulator) config_options_extras=
                     ;;

  esac

  export PATH="$ios_platform_dir/Developer/usr/bin:$PATH"
  export CC="gcc -isysroot $ios_sdk_dir -arch $arch"
  export CXX="g++ -isysroot $ios_sdk_dir -arch $arch"
  export CFLAGS="-Wno-trigraphs -Wreturn-type -Wunused-variable -I$gambit_prefix/include -L$gambit_prefix/lib"
  export CXXFLAGS="$CFLAGS"
  export LD="ld -arch $arch"
  export LDFLAGS=""
}

download_gambit_dist_tgz()
{
  gambit_dist="$gambit_dist_if_downloaded"
  update_with_latest_changes="$update_with_latest_changes_if_downloaded"

  major_minor="`echo \"$gambit_dist\" | sed -e \"s/gambc-\\([^_]*_[^_]*\\)\\(.*\\)/\\1/g\" -e \"s/_/./g\"`"

  curl "http://www.iro.umontreal.ca/~gambit/download/gambit/$major_minor/source/$gambit_dist.tgz" > "$gambit_dist.tgz"
}

get_gambit_dist_tgz()
{
  rootfromhere="`grep \"^rootfromhere = *\" makefile 2> /dev/null | sed -e \"s/rootfromhere = //\"`"
  gambit_dist="`grep \"^PACKAGE_TARNAME = *\" makefile 2> /dev/null | sed -e \"s/PACKAGE_TARNAME = *//\"`"

  if [ "$gambit_dist" == "" ]; then

    download_gambit_dist_tgz

    downloaded="yes"

  else

    (cd "$rootfromhere" ; make dist)
    mv "$rootfromhere/$gambit_dist.tgz" .

    update_with_latest_changes="no"
    downloaded="no"

  fi
}

unpack_gambit()
{
  dir="$1"
  rm -rf "$dir"
  tar zxf "$gambit_dist.tgz"
  mv "$gambit_dist" "$dir"
}  

configure_gambit()
{
  dir="$1"
  unpack_gambit "$dir"
  cd "$dir"
  ./configure --prefix="$gambit_prefix" $config_options_extras $config_options
  cd ..
}

make_gambit()
{
  dir="$1"
  cd "$dir"
  make clean
  make -j 2
  if [ "$update_with_latest_changes" == "yes" ]; then
    make update
    make -j 2
  fi
  make install
  cd ..
}

build_macosx()
{
  select_macosx

  configure_gambit "$gambit_dir"

  make_gambit "$gambit_dir"
}

build_one_ios()
{
  kind="$1"
  platform_type="$2"
  sdk_version="$3"
  arch="$4"

  select_ios "$kind" "$platform_type" "$sdk_version" "$arch"

  configure_gambit "$gambit_dir"

  make_gambit "$gambit_dir"

  for library in `(cd $gambit_prefix/lib; ls *.a)` ; do

    if [ -e "temp-lib/$library" ]; then

      # Combine the libraries of the various architectures into a universal library.

      lipo temp-lib/$library $gambit_prefix/lib/$library -output temp-lib/new-$library -create
      mv temp-lib/new-$library $gambit_prefix/lib/$library

    fi

    cp $gambit_prefix/lib/$library temp-lib

  done
}

build_all_ios()
{
  rm -rf temp-lib
  mkdir temp-lib

  for arch in i386 ; do
    build_one_ios "$ios_kind" Simulator "$ios_version" "$arch"
  done

  for arch in armv6 armv7 ; do
    build_one_ios "$ios_kind" OS "$ios_version" "$arch"
  done

  rm -rf temp-lib
}

# Get the Gambit distribution.

get_gambit_dist_tgz

# Build Gambit for all iOS platforms.

build_all_ios

# If you also want to build the Mac OS X version, then uncomment the
# following line:

#build_macosx
