#!/bin/bash
STARTDIR=/home/SMCDev/SMC/omniorb
OMNIVER=omniORB-4.1.7
OMNIFILE=$STARTDIR/$OMNIVER.tar.bz2
OMNIDIR=/home/SMCDev/SMC/omniorb/$OMNIVER
ARMDIR=$STARTDIR/armbuild
#ARMOUT=$STARTDIR/armoutput
ARMOUT=/home/SMCDev/omniORB/armoutput
ARMLOG=$STARTDIR/arm.log
ARMTCTEMP=$STARTDIR/armtctemp
NATIVEDIR=$STARTDIR/nativebuild
#NATIVEOUT=$STARTDIR/nativeoutput
NATIVEOUT=/home/SMCDev/omniORB/nativeoutput
NATIVELOG=$STARTDIR/native.log
XTC=/home/SMCDev/SMC/omniorb/armtoolchain

BUILD=1
CLEAN=0
DEBUG=0
REBUILD=0
NATIVE=0
ARM=0
ARCHSPEC=0

while [ -n "$1" ]
do
  if [ "$1" = "clean" ]
  then
    BUILD=0
    REBUILD=0
    CLEAN=1
  elif [ "$1" = "rebuild" ]
  then
    BUILD=0
    REBUILD=1
    CLEAN=0
  elif [ "$1" = "arm" ]
  then
    if [ $ARCHSPEC -eq 0 ]
    then
      ARCHSPEC=1
      NATIVE=0
    fi
    ARM=1
  elif [ "$1" = "native" ]
  then
    if [ $ARCHSPEC -eq 0 ]
    then
      ARCHSPEC=1
      ARM=0
    fi
    NATIVE=1
  elif [ "$1" = "debug" ]
  then
    DEBUG=1
  else
    echo "Unexpected option: $1"
    exit 1
  fi
  shift
done

echo "BUILD=$BUILD CLEAN=$CLEAN REBUILD=$REBUILD NATIVE=$NATIVE ARM=$ARM ARCHSPEC=$ARCHSPEC DEBUG=$DEBUG"

if [ $DEBUG -ne 0 ]
then
  ARMDIR="$ARMDIR-DEBUG"
  ARMOUT="$ARMOUT-DEBUG"
  NATIVEDIR="$NATIVEDIR-DEBUG"
  NATIVEOUT="$NATIVEOUT-DEBUG"
  OMNIDIR="$OMNIDIR-DEBUG"
fi

cd $STARTDIR

if [ ! -f $OMNIFILE ]
then
  echo "Missing archive file: $OMNIFILE"
  exit 1
fi

if [ $CLEAN -ne 0 ]
then
  echo "Cleaning pristine files"
  rm -rf $OMNIDIR
fi

if [ \( $CLEAN -ne 0 \) -o \( $REBUILD -ne 0 \) ]
then
  if [ \( $ARCHSPEC -eq 0 \) -o \( $NATIVE -ne 0 \) ]
  then
    echo "Cleaning native files"
    rm -rf $NATIVEDIR $NATIVEOUT
  fi
  if [ \( $ARCHSPEC -eq 0 \) -o \( $ARM -ne 0 \) ]
  then
    echo "Cleaning ARM files"
    rm -rf $ARMDIR $ARMOUT
  fi
fi

if [ \( $BUILD -ne 0 \) -o \( $REBUILD -ne 0 \) ]
then

  if [ ! -d $OMNIDIR ]
  then
    echo "Extracting pristine files from archive"
    mkdir --parents $OMNIDIR
    RETVAL="$?"
    if [ "$RETVAL" -ne 0 ]
    then
      echo "Failed to create output directory"
      exit 1
    fi
    cd $OMNIDIR
    tar xvf $OMNIFILE --strip-components=1
    RETVAL="$?"
    if [ "$RETVAL" -ne 0 ]
    then
      echo "Tarball extraction failed"
      exit 1
    fi
    if [ "$DEBUG" -ne 0 ]
    then
      for fn in `find $OMNIDIR/src/lib/omniORB -type f -print | xargs grep -l CXXDEBUGFLAGS`
      do
        echo "Editing $fn"
        #sed -e '1,$s/[#][\ \t]*CXXDEBUGFLAGS[\ \t]*[=][\ \t]*[-]g/CXXDEBUGFLAGS = -g/1' $fn
        (
          #echo '1,$s/[#][\ \t]*CXXDEBUGFLAGS[\ \t]*[=][\ \t]*[-]g/CXXDEBUGFLAGS = -O0 -g3/1'
          echo '1,$s/[#][\ \t]*CXXDEBUGFLAGS[\ \t]*[=][\ \t]*[-]g/CXXDEBUGFLAGS = -g/1'
          echo 'wq'
        ) | ed $fn
      done
    fi
    cd $STARTDIR
  fi
  
  if [ \( $ARCHSPEC -eq 0 \) -o \( $NATIVE -ne 0 \) ]
  then
    echo "*"
    echo "Making native build"
    echo "*"
    if [ ! -d $NATIVEDIR ]; then mkdir $NATIVEDIR ; fi
    if [ ! -d $NATIVEOUT ]; then mkdir $NATIVEOUT ; fi
    cd $NATIVEDIR
    echo "*"
    echo "* Performing initial native configuration"
    echo "*"
    $OMNIDIR/configure --prefix=$NATIVEOUT --disable-longdouble 2>&1 | tee $NATIVELOG
    echo "*"
    echo "* Building native omniORB"
    echo "*"
    make 2>&1 | tee -a $NATIVELOG
    echo "*"
    echo "* Installing native omniORB"
    echo "*"
    make install 2>&1 | tee -a $NATIVELOG
  fi

  if [ \( $ARCHSPEC -eq 0 \) -o \( $ARM -ne 0 \) ]
  then
    echo "*"
    echo "Making ARM build"
    echo "*"
    if [ ! -d $ARMDIR ]; then mkdir $ARMDIR ; fi
    if [ ! -d $ARMOUT ]; then mkdir $ARMOUT ; fi
    cd $ARMDIR
    (

      if false
      then

      if [ -d $ARMTCTEMP ]; then rm -rf $ARMTCTEMP ; fi
      mkdir $ARMTCTEMP
      for i in `ls $XTC/armv7l-timesys-linux-uclibcgnueabi/bin`
      do
        ln -s $XTC/armv7l-timesys-linux-uclibcgnueabi/bin/$i $ARMTCTEMP/armv7l-timesys-linux-$i
      done
      export PATH="$ARMTCTEMP:$PATH"
      echo $PATH
      if [ -n "$LD_LIBRARY_PATH" ]; then export LD_LIBRARY_PATH="$XTC/usr/lib:$LD_LIBRARY_PATH" ; else export LD_LIBRARY_PATH="$XTC/usr/lib" ; fi
      export CPATH="$XTC/usr/include"
      #/bin/bash
      #$OMNIDIR/configure --prefix=$ARMOUT --host=armv7l-timesys-linux-uclibcgnueabi --build=i686-pc-linux-gnu 2>&1 | tee $ARMLOG
      #$OMNIDIR/configure --prefix=$ARMOUT --host=armv7l-timesys-linux-uclibcgnueabi- --build=i686-pc-linux-gnu 2>&1 | tee $ARMLOG
      #CC=arm-linux-gcc-3.4 CXX=arm-linux-g++-3.4
      #$OMNIDIR/configure CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ --prefix=$ARMOUT --host=armv7l-timesys-linux --build=i686-pc-linux-gnu 2>&1 | tee $ARMLOG
      #$OMNIDIR/configure CC=armv7l-timesys-linux-gcc CXX=armv7l-timesys-linux-g++ --prefix=$ARMOUT --host=armv7l-timesys-linux --build=i686-pc-linux-gnu 2>&1 | tee $ARMLOG
      # perform these steps to get X86 version of tools
      make CC=gcc -C src/tool/omniidl/cxx/cccp
      make CXX=g++ -C src/tool/omniidl/cxx
      make CC=gcc -C src/tool/omkdepend
      # now build for ARM
      make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ 2>&1 | tee -a $ARMLOG
      make 2>&1 | tee -a $ARMLOG
      #make install 2>&1 | tee -a $ARMLOG

      else

      (
        if [ -d $ARMTCTEMP ]; then rm -rf $ARMTCTEMP ; fi
        mkdir $ARMTCTEMP
        for i in `ls $XTC/armv7l-timesys-linux-uclibcgnueabi/bin`
        do
          ln -s $XTC/armv7l-timesys-linux-uclibcgnueabi/bin/$i $ARMTCTEMP/armv7l-timesys-linux-$i
        done

        echo "*"
        echo "* Performing initial ARM configuration"
        echo "*"
        export PATH="$XTC/usr/bin:$ARMTCTEMP:$PATH"
        export LIBRARY_PATH=$XTC/usr/lib
        export CPATH="$XTC/usr/include"
        #export omni_cv_cxx_catch_by_base_set=set
        #export omni_cv_cxx_catch_by_base=yes
        $OMNIDIR/configure CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ --prefix=$ARMOUT --host=arm-timesys-linux --build=i686-pc-linux-gnu --disable-longdouble
        # perform these steps to get X86 version of tools
        echo "*"
        echo "* Building tools in native mode so that they can be executed here"
        echo "*"
        export LIBRARY_PATH=
        export CPATH=
        make CC=gcc -C src/tool/omniidl/cxx/cccp
        make CXX=g++ -C src/tool/omniidl/cxx
        make CC=gcc -C src/tool/omkdepend
        # now build for ARM
        echo "*"
        echo "* Completing first pass build for ARM"
        echo "*"
        export LIBRARY_PATH=$XTC/usr/lib
        export CPATH="$XTC/usr/include"
	#export IMPORT_CPPFLAGS=-fvolatile
        make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++
        #export IMPORT_CPPFLAGS=
        # force a rebuild of some tools so that they are usable on ARM
        echo "*"
        echo "* Rebuilding tools for ARM so that they can be executed on target"
        echo "*"
        rm -f lib/_omniidlmodule.so* lib/omnicpp
        make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ -C src/tool/omniidl/cxx clean
        make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ -C src/tool/omniidl/cxx
        make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++
        make CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ install
      ) 2>&1 | tee $ARMLOG

      fi
    )
  fi

fi

