#!/bin/bash
STARTDIR=/home/SMCDev/SMC/valgrind
PROGNAME=valgrind
PROGVER=valgrind-3.6.1
PROGFILE=$STARTDIR/$PROGVER.tar.bz2
PROGDIR=$PROGVER
ARMDIR=$STARTDIR/armbuild
if [ -z "$ARMOUT" ]; then ARMOUT=$STARTDIR/armoutput ; fi
if [ -z "$ARMOUTFILE" ]; then ARMOUTFILE=$STARTDIR/armoutput.tar.bz2 ; fi
ARMLOG=$STARTDIR/arm.log
ARMTCTEMP=$STARTDIR/armtctemp
NATIVEDIR=$STARTDIR/nativebuild
if [ -z "$NATIVEOUT" ]; then NATIVEOUT=$STARTDIR/nativeoutput ; fi
NATIVELOG=$STARTDIR/native.log
XTC=$STARTDIR/armtoolchain

if [ ! -d "$XTC" ]
then
  echo "Missing toolchain directory or symbolic link at $XTC"
  exit 1
fi

BUILD=1
CLEAN=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
  else
    echo "Unexpected option: $1"
    exit 1
  fi
  shift
done

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

cd $STARTDIR

if [ ! -f $PROGFILE ]
then
  echo "Missing archive file: $PROGFILE"
  exit 1
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 $ARMTCTEMP
  fi
fi

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

  
  if [ \( $ARCHSPEC -eq 0 \) -o \( $NATIVE -ne 0 \) ]
  then
  if [ ! -d $NATIVEDIR ]; then mkdir $NATIVEDIR ; fi
    cd $NATIVEDIR
    if [ ! -d $PROGDIR ]
    then
      echo "*"
      echo "Extracting pristine files from archive to native directory"
      echo "*"
      tar xvf $PROGFILE
    fi
    echo "*"
    echo "* Performing initial native configuration"
    echo "*"
    cd $NATIVEDIR/$PROGDIR
    ./configure --prefix=$NATIVEOUT 2>&1 | tee $NATIVELOG
    echo "*"
    echo "* Building native $PROGNAME"
    echo "*"
    make 2>&1 | tee -a $NATIVELOG
    echo "*"
    echo "* Installing native $PROGNAME"
    echo "*"
    sudo make install 2>&1 | tee -a $NATIVELOG
  fi

  if [ \( $ARCHSPEC -eq 0 \) -o \( $ARM -ne 0 \) ]
  then
    if [ ! -d $ARMDIR ]; then mkdir $ARMDIR ; fi
    cd $ARMDIR
    if [ ! -d $PROGDIR ]
    then
      echo "*"
      echo "Extracting pristine files from archive to ARM directory"
      echo "*"
      tar xvf $PROGFILE
    fi
    echo "*"
    echo "Making ARM build"
    echo "*"
    if [ ! -d $ARMDIR ]; then mkdir $ARMDIR ; fi
    if [ ! -d $ARMOUT ]; then mkdir $ARMOUT ; fi
    NumFiles=`find $ARMOUT -print | wc -l`
    if [ "$NumFiles" != "1" ]; then echo "Output directory $ARMOUT is not empty" ; exit 1 ; fi
    cd $ARMDIR/$PROGDIR
      (
        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"
        MYARGS="CC=armv7l-timesys-linux-uclibcgnueabi-gcc CXX=armv7l-timesys-linux-uclibcgnueabi-g++ RANLIB=$XTC/armv7l-timesys-linux-uclibcgnueabi/bin/ranlib"
        ./configure $MYARGS --prefix=$ARMOUT --host=armv7l-timesys-linux --build=i686-pc-linux-gnu
        # now build for ARM
        echo "*"
        echo "* Completing build for ARM"
        echo "*"
        make $MYARGS
	echo "*"
	echo "* Installing ARM $PROGNAME"
	echo "*"
        echo PATH=$PATH
        echo LIBRARY_PATH=$LIBRARY_PATH
        sudo make $MYARGS install
        echo "*"
        echo "* Writing local archive"
        echo "*"
        tar cvjf $ARMOUTFILE `find $ARMOUT -type f -print`
      ) 2>&1 | tee $ARMLOG
    fi
fi

