#!/bin/sh

. buildsys/functions

ProcessArgs $*
Assert LoadConfigs
Assert SetupLogDir
Assert SetupResFile

BASE=`/bin/pwd`
CURRENT_PL_LINUX=`uname | grep Linux`
CURRENT_PL_CYGWIN=`uname | grep CYGWIN`
CURRENT_PL_AIX=`uname | grep AIX`
CURRENT_PL_SUNOS=`uname | grep SunOS`
IS_WINDOWS_EXE="false"  


cd ../../..

SetState BuildingExamples
echo "BUILD/EXAMPLES=RUNNING" >> $RESFILE
ArchiveLogs

check_platform()
{

  if [ "$CURRENT_PL_LINUX" != "" -o "$CURRENT_PL_AIX" != "" -o "$CURRENT_PL_SUNOS" != "" ] ;then
    return 0
  elif [ "$CURRENT_PL_CYGWIN" != "" ];then
    return 1
  else 
   echo UNKNOWN Platform:
   echo Current platform: `uname`
   exit
  fi
}

if [ "$SETUP_TYPE" = "pcx86.int509-dev" -o "$SETUP_TYPE" = "pcx86.int509-release" ]
then
   if [ "$DEP" != "none" ]
   then
      OSPL_HOME=$DEPWORKDIR
      export OSPL_HOME
   fi

   SPLICE_PLATFORM=$SETUP_TYPE
   export SPLICE_PLATFORM
   . ./configure -full
   SPLICE_ORB=DDS_OpenFusion_1_6_1
   export SPLICE_ORB

   if [ "$CONFIGURATION" != "OK" ]
   then
      echo  "ERROR: configuration - dcps_build_examples"
      exit 1
   fi

   ArchiveLogs

    . $CONFIGFILE
   cd $LOGDIR/examples/build

   $OSPL_OUTER_HOME/testsuite/bin/build_examples_pcx86_integrity509
   exit $?
else

    # Install a copy
    if [ "$DEP" = "none" ]
    then
        INSTALLERDIR=$WORKDIR/build/install
        . $WORKDIR/build/release_info/RELEASE
    else
        echo "Setting OUTER_BASE to $BASE..."
        OUTER_BASE=$BASE
        BASE=$DEPWORKDIR/build/scripts/overnight
        INSTALLERDIR=$DEPWORKDIR/install
        . $DEPWORKDIR/release_info/RELEASE
    fi

    echo "Installing..."
    SHORTSETUP=`echo $SETUP_TYPE | sed 's/-release//'`
    if [ -f $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE-installer.bin ]
    then
       echo "Installing $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE-installer.bin into $WORKDIR/installed/"

       $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE-installer.bin --mode unattended --prefix $WORKDIR/installed/
#--providedLicenseFile licfile
    else
        echo "$INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE-installer.bin does not exist"

        if [ -f $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.tar ]
        then
            echo "Untarring $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.tar ..."
            mkdir $WORKDIR/installed
            (
              cd $WORKDIR/installed/ && \
              tar -xvf $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.tar
              mv HDE/$SHORTSETUP/release.com \
                  HDE/$SHORTSETUP/release.com.orig
              sed < HDE/$SHORTSETUP/release.com.orig \
                  > HDE/$SHORTSETUP/release.com \
                  "s%@@INSTALLDIR@@%$WORKDIR/installed/%"
            )
        else

            if [ -f $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.zip ]
            then
                echo "Unzipping $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.zip ..."
                mkdir $WORKDIR/installed
                (
                  cd $WORKDIR/installed/ && \
                  unzip $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE.zip
                )
            fi
        fi
    fi
        
    # If its a windows build with an exe file then the installation doesn't work with the exe file
    # at the minute.  So use the HDE from the build/install directory instead    
    if [ -f $INSTALLERDIR/VC/OpenSpliceDDS$PACKAGE_VERSION-$SHORTSETUP-HDE-installer.exe ]
    then
        IS_WINDOWS_EXE="true"
        echo "Copying (1) $BASE/example_automation_scripts/* $INSTALLERDIR/HDE/$SHORTSETUP/ ...."

        cp -rp $BASE/example_automation_scripts/* $INSTALLERDIR/HDE/$SHORTSETUP/

        echo "DEP is $DEP....."

        if [ "$DEP" != "none" ]
        then
            echo "Copying (2) $WORKDIR/build/build/scripts/overnight/example_automation_scripts/* $INSTALLERDIR/HDE/$SHORTSETUP/ ...."  
            cp -rp $WORKDIR/build/build/scripts/overnight/example_automation_scripts/* $INSTALLERDIR/HDE/$SHORTSETUP/
        fi                   
    else 
        echo "Copying (1) $BASE/example_automation_scripts/* $WORKDIR/installed/HDE/$SHORTSETUP/ ...."

        cp -rp $BASE/example_automation_scripts/* $WORKDIR/installed/HDE/$SHORTSETUP/

        echo "DEP is $DEP....."

        if [ "$DEP" != "none" ]
        then
            echo "Copying (2) $WORKDIR/build/build/scripts/overnight/example_automation_scripts/* $WORKDIR/installed/HDE/$SHORTSETUP/ ...."  
            cp -rp $WORKDIR/build/build/scripts/overnight/example_automation_scripts/* $WORKDIR/installed/HDE/$SHORTSETUP/
        fi
    fi

    if [ $? = 0 ]
    then
        echo "Install completed"
    else
        echo "ERROR: Install failed"
        exit 1
    fi
    
    if check_platform
    then
        cd $WORKDIR/installed/HDE/$SHORTSETUP/
        . ./release.com    
    else
       if [ "$IS_WINDOWS_EXE" = "true" ]
       then 
          echo "Windows exe file found changing to $INSTALLERDIR/HDE/$SHORTSETUP/"
          cd $INSTALLERDIR/HDE/$SHORTSETUP/
       else
          cd $WORKDIR/installed/HDE/$SHORTSETUP/
       fi
    fi

    cd examples
    if [ $? != 0 ]
    then
        echo "ERROR: Couldn't locate examples direcctory in installed HDE."
        exit 1
    fi

    # Do not do CORBA C++ and standalone C++ examples with AIX for now as the build fails
    if [ "$SETUP_TYPE" = "PPC5plus.AIX5.3-dev" -o "$SETUP_TYPE" = "PPC5plus.AIX5.3-release" ]
    then
        EXAMPLES="dcps/CORBA/Java/JacORB/PingPong dcps/CORBA/Java/JacORB/Tutorial dcps/standalone/C/PingPong dcps/standalone/C/Tutorial dcps/standalone/C++OnC/PingPong dcps/standalone/Java/PingPong dcps/standalone/Java/Tutorial "
    else
        EXAMPLES="dcps/CORBA/Java/JacORB/PingPong dcps/CORBA/Java/JacORB/Tutorial dcps/CORBA/C++/OpenFusion/PingPong dcps/CORBA/C++/OpenFusion/Tutorial dcps/standalone/C/PingPong dcps/standalone/C/Tutorial dcps/standalone/C++/PingPong dcps/standalone/C++/Tutorial dcps/standalone/C++OnC/PingPong dcps/standalone/Java/PingPong dcps/standalone/Java/Tutorial "

        # DLRL examples are not included in an inner only build so only add them if doing an outer build and
        # they shouldn't be tested if it's AIX either
        if [ "$DEP" != "none" ]
        then
            # There is a problem with the file name being too long if it's windows OSPLO build and 
            # we are using the HDE from the build/install directory.  So don't do the JAVA DLRL 
            # example for now if it's a windows OSPLO build
            if [ "$IS_WINDOWS_EXE" = "false" ]
            then
                EXAMPLES=$EXAMPLES" dlrl/standalone/Java/Tutorial "
            fi

            EXAMPLES=$EXAMPLES" dlrl/standalone/C++/Tutorial "
        fi
    fi
 
    export EXAMPLES

    #These examples have special files for build: BUILD(or BUILD.bat for Windows)
    SPECIAL_BUILD_FILES="dcps/CORBA/Java/JacORB/PingPong dcps/CORBA/Java/JacORB/Tutorial dcps/standalone/Java/PingPong dcps/standalone/Java/Tutorial dlrl/standalone/Java/Tutorial dlrl/standalone/C++/Tutorial "
    export SPECIAL_BUILD_FILES

    if [ "$SETUP_TYPE" = "PPC5plus.AIX5.3-dev" -o "$SETUP_TYPE" = "PPC5plus.AIX5.3-release" ]
    then
       PATH="/usr/vac/bin:/usr/vacpp/bin:$PATH"
    fi

    echo "Build Examples:"
    if check_platform
    then
        PATH="$JAVA_HOME/bin:$PATH"
        PATH="$PATH:$TAO_ROOT/bin:$JACORB_HOME/bin"

        LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$TAO_ROOT/lib"

        SPLICE_ORB=DDS_OpenFusion_1_6_1
         
        CLASSPATH="$CLASSPATH:$JACORB_HOME/lib/endorsed/jacorb.jar:$JACORB_HOME/lib/endorsed/logkit.jar:$JACORB_HOME/lib/idl.jar"

        export PATH LD_LIBRARY_PATH SPLICE_ORB CLASSPATH

        sh $BASE/build_examples.sh
    else
        PATH="$PATH:/cygdrive/c/WINDOWS/system32"
        export PATH
        
        if [ "$DEP" != "none" ]
        then
            if [ "$IS_WINDOWS_EXE" = "true" ]
            then
                XPATH="$(cygpath -w $INSTALLERDIR/HDE/$SHORTSETUP)"
            else
               XPATH="$(cygpath -w $OSPL_HOME)"
            fi
        else
            XPATH="$(cygpath -w $WORKDIR/installed/HDE/$SHORTSETUP)"
        fi

        echo "set OSPL_HOME=$XPATH" > setenv.bat
        
        XPATH=`cygpath -w "$TAO_ROOT"`
        echo "set TAO_ROOT=$XPATH" >> setenv.bat

        XPATH=`cygpath -w "$JACORB_HOME"`
        echo "set JACORB_HOME=$XPATH" >> setenv.bat

        echo "set SPLICE_ORB=DDS_OpenFusion_1_6_1" >> setenv.bat
        
        echo "set JAVA_HOME=$JAVA_HOME" >> setenv.bat

        echo "set PATH=%JAVA_HOME%\bin;%TAO_ROOT%\bin;%TAO_ROOT%\lib;%PATH%;%JACORB_HOME%\bin" >> setenv.bat

        echo "set CLASSPATH=%CLASSPATH%;%JACORB_HOME%\lib\endorsed\jacorb.jar;%JACORB_HOME%\lib\endorsed\logkit.jar;%JACORB_HOME%\lib\idl.jar" >> setenv.bat

        if [ $HOSTNAME == "DDS1-WIN2K3" ]
        then
            VS80DIR="C:\Program Files\Microsoft Visual Studio 8\Common7\Tools"
            echo "set VS80COMNTOOLS=$VS80DIR" >> setenv.bat
        else
            if [ $HOSTNAME == "dds2-win2k3" ]
            then
                VS90DIR="C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools"
                echo "set VS90COMNTOOLS=$VS90DIR" >> setenv.bat
            fi
        fi

        XPATH=`cygpath -w "$BASE"`
        cmd.exe /c $XPATH/build_examples.bat

    fi
    exit $?
fi
