#!/bin/sh
#
# Bootstrap script for IWLS 2007 synthesis challenge.
#
# Author: Florian Krohm fkrohm@us.ibm.com
#
# Usage:    ./bootstrap 
#
#
set -e

if [ ! -f ./bootstrap ]; then
  echo "*** You are invoking 'bootstrap' from a wrong directory. You"   1>&2
  echo "    must invoke 'bootstrap' in the directory that contains it." 1>&2
  exit 1
fi

#################################################################
# Tarballs
#################################################################
zlib_tarball="zlib-1.2.3.tar.gz"
make_tarball="make-3.80.tar.gz"
m4_tarball="m4-1.4.tar.gz"
flex_tarball="flex-2.5.4a.tar.gz"
bison_tarball="bison-1.875.tar.gz"
patch_tarball="patch-2.5.4.tar.gz"
tcl_tarball="tcl8.4.11-src.tar.gz"
perl_tarball="perl-5.8.8.tar.gz"
automake_tarball="automake-1.9.6.tar.gz"
autoconf_tarball="autoconf-2.59.tar.gz"
cvs_tarball="cvs-1.11.21.tar.gz"
cudd_tarball="cudd-2.4.1.tar.gz"
oa_i686_bin_tarball="oaDev_2.2p052_linux_rhel21.tar.gz"
oa_x86_64_bin_tarball="oaDev_2.2p052_linux_rhel30.tar.gz"
oa_inc_tarball="oaInc_2.2p052_all.tar.gz"
oa_doc_tarball="oaDoc_2.2p052_all.tar.gz"
oa_bin_license="LICENSE.pdf"
oagear_tarball="OAGear-0.98a.tar.gz"
infrastructure_tarball="iwls-2008.tar.gz"
libtool_tarball="libtool-1.5.22.tar.gz"

#################################################################
# Directories (absolute pathnames)
#################################################################
root_dir="`/bin/pwd`"
tarball_dir="$root_dir/tarballs"
patches_dir="$root_dir/patches"
install_dir="$root_dir/install"
bin_dir="$install_dir/bin"
lib_dir="$install_dir/lib"
oagear_dir="$root_dir/OAGear-0.98a"
cudd_dir="$root_dir/`basename $cudd_tarball .tar.gz`"
tcl_dir="$install_dir"
oa_dir_name="OA-2.2p052"
oa_dir="$root_dir/$oa_dir_name"


#################################################################
# Check platform
#################################################################
if [ `uname -s` != "Linux" ]; then
  echo "*** Unsupported platform" 1>&2
  exit 1
fi
rev=""
case `uname -m` in
  ia64) echo "*** ia64 (Itanium) is not supported" 1>&2
        exit 1
        ;;
  x86_64)
        rev="x86_64"
        ;;
  i686) rev="i686"
        ;;
  *) echo "*** Unsupported hardware `uname -m`" 1>&2
     exit 1
     ;;
esac

#################################################################
# Move tarballs
#################################################################
if [ ! -f "$tarball_dir/$oa_inc_tarball" ]; then
  if [ ! -f "$oa_inc_tarball" ]; then
    echo "*** $oa_inc_tarball not found; please download it" 1>&2
    exit 1
  fi
fi
if [ ! -f "$tarball_dir/$oa_doc_tarball" ]; then
  if [ ! -f "$oa_doc_tarball" ]; then
    echo "*** $oa_doc_tarball not found; please download it" 1>&2
    exit 1
  fi
fi

case $rev in
  i686)
    if [ ! -f "$tarball_dir/$oa_i686_bin_tarball" ]; then
      if [ ! -f "$oa_i686_bin_tarball" ]; then
        echo "*** $oa_i686_bin_tarball not found; please download it" 1>&2
        exit 1
      fi
    fi
    ;;
  x86_64)
    if [ ! -f "$tarball_dir/$oa_x86_64_bin_tarball" ]; then
      if [ ! -f "$oa_x86_64_bin_tarball" ]; then
        echo "*** $oa_x86_64_bin_tarball not found; please download it" 1>&2
        exit 1
      fi
    fi
    ;;
esac

if [ ! -f "$tarball_dir/$oagear_tarball" ]; then
  if [ ! -f "$oagear_tarball" ]; then
    echo "*** $oagear_tarball not found; please download it" 1>&2
    exit 1
  fi
fi

if [ -f "$oa_inc_tarball" ]; then
  mv "$oa_inc_tarball" "$tarball_dir"
fi
if [ -f "$oa_doc_tarball" ]; then
  mv "$oa_doc_tarball" "$tarball_dir"
fi
if [ -f "$oa_i686_bin_tarball" ]; then
  mv "$oa_i686_bin_tarball" "$tarball_dir"
fi
if [ -f "$oa_x86_64_bin_tarball" ]; then
  mv "$oa_x86_64_bin_tarball" "$tarball_dir"
fi
if [ -f "$oagear_tarball" ]; then
  mv "$oagear_tarball" "$tarball_dir"
fi
if [ -f "$infrastructure_tarball" ]; then
  mv "$infrastructure_tarball" "$tarball_dir"
fi

#################################################################
# Files (absolute pathnames)
#################################################################
iwls_setup_sh_file_name="iwls-setup.sh"
iwls_setup_sh_file="$root_dir/$iwls_setup_sh_file_name"
iwls_setup_csh_file_name="iwls-setup.csh"
iwls_setup_csh_file="$root_dir/$iwls_setup_csh_file_name"

#################################################################
# Determine platform
#################################################################
case $rev in 
  i686) sysname="linux_UNKNOWN_32"
        oa_unsupported_plat="linux_UNKNOWN"
        ;;
  x86_64) sysname="linux_UNKNOWN_64"
        oa_unsupported_plat="linux_UNKNOWN"
        ;; 
esac

#################################################################
# Generate the 'iwls-setup.sh' script.
# If it exists, do not remove it because it could have been
# edited, e.g. to fix LD_LIBRARY_PATH.
#################################################################
if [ ! -f "$iwls_setup_sh_file" ]; then
  touch "$iwls_setup_sh_file"

#  if [ "x$oa_unsupported_platform" != "x" ]; then
#    echo "OA_UNSUPPORTED_PLAT=$oa_unsupported_plat" >> "$iwls_setup_sh_file"
#    echo "export OA_UNSUPPORTED_PLAT" >> "$iwls_setup_sh_file"
#    echo " " >> "$iwls_setup_sh_file"
#  fi
  cat >>  "$iwls_setup_sh_file" <<EOF1
#  
# IWLS_HOME must be the absolute path to the directory containing this file.
# Therefore, if you move this directory around you need to adjust IWLS_HOME.
#
IWLS_HOME="$root_dir"
EOF1

  cat >>  "$iwls_setup_sh_file" <<EOF
ZLIB_HOME="\$IWLS_HOME/install"
BISON_HOME="\$IWLS_HOME/install"
FLEX_HOME="\$IWLS_HOME/install"
TCL_HOME="\$IWLS_HOME/install"
PATH="\$IWLS_HOME/install/bin:\$IWLS_HOME/$oa_dir_name/bin:\$PATH"
OA_MODE=dbg
LD_LIBRARY_PATH="\$IWLS_HOME/$oa_dir_name/lib/$sysname/\$OA_MODE:\$LD_LIBRARY_PATH"
OA_DIR="\$IWLS_HOME/$oa_dir_name"

export IWLS_HOME
export ZLIB_HOME
export BISON_HOME
export FLEX_HOME 
export TCL_HOME
export PATH 
export OA_MODE
export LD_LIBRARY_PATH
export OA_DIR
EOF
fi

#################################################################
# Generate the 'iwls-setup.csh' script.
# If it exists, do not remove it because it could have been
# edited, e.g. to fix LD_LIBRARY_PATH.
#################################################################

if [ ! -f "$iwls_setup_csh_file" ]; then
  touch "$iwls_setup_csh_file"

  cat >>  "$iwls_setup_csh_file" <<EOF
#  
# IWLS_HOME must be the absolute path to the directory containing this file.
# Therefore, if you move this directory around you need to adjust IWLS_HOME.
#
set IWLS_HOME="$root_dir"
set ZLIB_HOME="\$IWLS_HOME/install"
set BISON_HOME="\$IWLS_HOME/install"
set FLEX_HOME="\$IWLS_HOME/install"
set TCL_HOME="\$IWLS_HOME/install"
set PATH="\$IWLS_HOME/install/bin:\$IWLS_HOME/$oa_dir_name/bin:\$PATH"
set OA_MODE=dbg
set OA_DIR="\$IWLS_HOME/$oa_dir_name"

setenv IWLS_HOME "\$IWLS_HOME"
setenv ZLIB_HOME "\$ZLIB_HOME"
setenv BISON_HOME "\$BISON_HOME"
setenv FLEX_HOME "\$FLEX_HOME"
setenv TCL_HOME "\$TCL_HOME"
setenv PATH "\$PATH" 
setenv OA_MODE "\$OA_MODE"
setenv OA_DIR "\$OA_DIR"

set TEST=\$?LD_LIBRARY_PATH

if ("\$TEST" == "0") then
  set LD_LIBRARY_PATH="\$IWLS_HOME/$oa_dir_name/lib/$sysname/\$OA_MODE"
  setenv LD_LIBRARY_PATH "\$LD_LIBRARY_PATH"
else  
  set LD_LIBRARY_PATH="\$IWLS_HOME/$oa_dir_name/lib/$sysname/\${OA_MODE}:\$LD_LIBRARY_PATH"
  setenv LD_LIBRARY_PATH "\$LD_LIBRARY_PATH"
endif
EOF
fi

#################################################################
# Source the 'setup-env' script
#################################################################
. ./"$iwls_setup_sh_file_name"


#################################################################
# Create the 'install' directory
#################################################################
mkdir -p "$install_dir"

echo '###########################################################'
echo '#  Step I: installing development tools (ignore warnings)'
echo '###########################################################'

#################################################################
# 1) Build zlib.a
#################################################################
cd "$root_dir"
if [ ! -f "$lib_dir/libz.a" ]; then
  echo "...building libz.a"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$zlib_tarball" | tar xf -
  cd `basename $zlib_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 2) Build make
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/make" ]; then
  echo "...building make"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$make_tarball" | tar xf -
  cd `basename $make_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 3) Build flex
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/flex" ]; then
  echo "...building flex"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$flex_tarball" | tar xf -
  cd `basename $flex_tarball a.tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi 

#################################################################
# 4) Build patch
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/patch" ]; then
  echo "...building patch"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$patch_tarball" | tar xf -
  cd `basename $patch_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 5) Build m4
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/m4" ]; then
  echo "...building m4"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$m4_tarball" | tar xf -
  cd `basename $m4_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 6) Build bison
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/bison" ]; then
  echo "...building bison"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$bison_tarball" | tar xf -
  cd `basename $bison_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
# The bison template needs a patch because g++ version 3.3.5 does not
# allow __attribute__(unused) for labels.
  cd "$install_dir/share/bison"
  patch < "$patches_dir/bison-template-patch"
fi

#################################################################
# 7) Build autoconf
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/autoconf" ]; then
  echo "...building autoconf"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$autoconf_tarball" | tar xf -
  cd `basename $autoconf_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log
  make > MAKE.log
  make install > INSTALL.log
  cd ../..
  rm -rf ./build
fi

#################################################################
# 8) Build automake
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/automake" ]; then
  echo "...building automake"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$automake_tarball" | tar xf -
  cd `basename $automake_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 9) Build libtool
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/libtool" ]; then
  echo "...building libtool"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$libtool_tarball" | tar xf -
  cd `basename $libtool_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 10) Build tcl
#################################################################
cd "$root_dir"
if [ ! -f "$lib_dir/tclConfig.sh" ]; then
  echo "...building Tcl"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$tcl_tarball" | tar xf -
  cd `basename $tcl_tarball -src.tar.gz`/unix
  patch < "$patches_dir/tcl-configure-patch"
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../../..
  rm -rf ./build
fi

#################################################################
# 11) Build perl
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/perl" ]; then
  echo "...building perl"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$perl_tarball" | tar xf -
  cd `basename $perl_tarball .tar.gz`
  patch < "$patches_dir/perl-makedepend-patch"
# configure.gnu exits with 1 in afs because changing the permissions
# of a particular file fails. But that is harmless and can be ignored.
  set +e
  ./configure.gnu --prefix="$install_dir" > CONFIG.log 2>&1
  set -e
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build
fi

#################################################################
# 12) Build cvs
#################################################################
cd "$root_dir"
if [ ! -f "$bin_dir/cvs" ]; then
  echo "...building cvs"
  mkdir -p build
  cd build
  gunzip -c "$tarball_dir/$cvs_tarball" | tar xf -
  cd `basename $cvs_tarball .tar.gz`
  ./configure --prefix="$install_dir" > CONFIG.log 2>&1
  make > MAKE.log 2>&1
  make install > INSTALL.log 2>&1
  cd ../..
  rm -rf ./build  
fi

#################################################################
# 13) Build CUDD
#################################################################
cd "$root_dir"
if [ ! -d "$cudd_dir" ]; then
  echo "...building cudd"
  gunzip -c "$tarball_dir/$cudd_tarball" | tar xf -
  cd "$cudd_dir"
 case $rev in
  x86_64)
    sed -e "s/XCFLAGS	= -mcpu=pentiumpro -malign-double -DHAVE_IEEE_754 -DBSD/#XCFLAGS	= -mcpu=pentiumpro -malign-double -DHAVE_IEEE_754 -DBSD/" Makefile > Makefile2
    sed -e "s/#XCFLAGS	= -DHAVE_IEEE_754 -DBSD -DSIZEOF_VOID_P=8 -DSIZEOF_LONG=8/XCFLAGS	= -DHAVE_IEEE_754 -DBSD -DSIZEOF_VOID_P=8 -DSIZEOF_LONG=8/" Makefile2 > Makefile
  esac
  if [ "$CC" ]; then
    cudd_opts="CC=$CC"
  else
    cudd_opts=""
  fi
  if [ "$CXX" ]; then
    cudd_opts="$cudd_opts CPP=$CXX"
  fi
  make $cudd_opts > MAKE.log 2>&1
  rm -f MAKE.log 
fi

echo '###########################################################'
echo '#  Step I: finished'
echo '###########################################################'
echo '###########################################################'
echo '#  Step II: installing OA'
echo '###########################################################'

#################################################################
# 14) Build lef and def
#################################################################
# not needed

#################################################################
# 15) Extract OA and apply patches
#################################################################
cd "$root_dir"
mkdir -p "$oa_dir"
cd "$oa_dir"
if [ ! -d "doc" ]; then
  gunzip -c "$tarball_dir/$oa_doc_tarball" | tar xf - 2> /dev/null
fi
if [ ! -d "bin" ]; then
  echo "...installing OA"
  cp "$tarball_dir/$oa_bin_license" "$oa_dir"
  case $rev in
  i686) gunzip -c "$tarball_dir/$oa_i686_bin_tarball" | tar xf - 2> /dev/null
        ;;
  x86_64) gunzip -c "$tarball_dir/$oa_x86_64_bin_tarball" | tar xf - 2> /dev/null
        ;;
  esac
  cd bin
  patch < "$patches_dir/sysname-2.2p052-patch"
  cd ..
  case $rev in
  i686) (cd bin; ln -s linux_rhel21_32 "$sysname")
        (cd lib; ln -s linux_rhel21_32 "$sysname")
        ;;
  x86_64) (cd bin; ln -s linux_rhel30_64 "$sysname")
          (cd lib; ln -s linux_rhel30_64 "$sysname")
          ;;
  esac
fi
if [ ! -d "include" ]; then
  gunzip -c "$tarball_dir/$oa_inc_tarball" | tar xf - 2> /dev/null
fi

echo '###########################################################'
echo '#  Step II: finished'
echo '###########################################################'
echo '###########################################################'
echo '#  Step III: installing OAGear'
echo '###########################################################'

#################################################################
# 16) Get OAGear
#################################################################
cd "$root_dir"
if [ ! -d "$oagear_dir" ]; then
#echo "...checking out OAGear from CVS"
#cvs -z3 -d:pserver:anonymous@cvs.openedatools.si2.org:/cvsroot/oagear co .

  echo "...extracting OAGear tarball"
  gunzip -c "$tarball_dir/$oagear_tarball" | tar xf - 2> /dev/null
fi


#################################################################
# 17) Create configure
#################################################################
cd "$oagear_dir"

echo "...moving configure to configure_old"
mv configure configure_old

echo "...creating configure"
Build/setup-developer > SETUP.LOG 2>&1

#################################################################
# 18) Build OAGear
#################################################################

 
#
# We need to specify --enable-debug otherwise the liboaBase.so DSO
# won't be found.

echo "...configuring OAGear" 
CFLAGS='-g' CXXFLAGS='-g' \
./configure --prefix="$root_dir/install" \
            --with-cudd="$cudd_dir" \
            --with-oa="$oa_dir" \
            --with-sysname="$sysname" \
            --with-tcl="$tcl_dir" \
            --disable-capo-wrapper \
            --enable-shared \
            --disable-static \
            --disable-bazaar --enable-debug > CONFIG.log 2>&1

echo "...building OAGear"
make > MAKE.LOG 2>&1

cd "$oagear_dir"

echo "...running some OAGear tests"
make check > CHECK.LOG 2>&1

if grep -q ".*of.*tests.failed" CHECK.LOG
then echo "At least one OAGear tests failed! Installation not working. See CHECK.LOG for more information."
else
echo "All OAGear tests ran successfully."
echo "Removing configure_old"
rm -f configure_old
fi

echo "...installing OAGear"
make install > INSTALL.log 2>&1 
# rm -f CONFIG.log MAKE.log INSTALL.log

echo '###########################################################'
echo '#  Step III: finished'
echo '###########################################################'

#################################################################
# Done. Phew.
#################################################################
echo "...bootstrap finished"
exit 0
