#!/bin/bash

# Simple Unix shell script to build STX:LIBJAVA using Jenkins CI
# Actually, it is used on Windows (with UnxUtils) as well.

PROJECT="STX_LIBJAVA"

# Determine operating system, bit simple but works
if [ "$OS" == "Windows_NT" ]; then
    WIN32=1
    UNIX=0
else
    WIN32=0
    UNIX=1
fi

if [ "$1" == "-tests" ]; then
    SKIP_CHECKOUT=yes
    SKIP_COMPILE=yes
    SKIP_ARTIFACTS=yes

    RUN_REPORTS=yes
fi




# Setup...
if [ "$WIN32" == "1" ]; then
    # WINDOWS
    # MUST export this, otherwise libjpeg fails to build!!!"
    export BCB="C:\Borland\BCC55"
    # To make cvs/ssh happy
    export CVS_RSH=plink
    # Set home
    export HOME=$USERPROFILE
    # Defaults
    export CVSROOT=":pserver:jenkinsBuild:blablu@cvs.bh.exept.de:/cvs/stx"
    # Make using bmake here..."
    MAKE="cmd /C bmake.bat"
else
    # LINUX
    # to get Mercurial
    export PATH=$PATH:/home/vrany/bin
    # Make using regular make here..."
    MAKE=make
    # Defaults
    export CVSROOT="/cvs/stx"
fi



# Sorry, it's lot easier for JV to have all the messages in EN :-)
export LANG=en_GB.UTF-8


# When run interactively, Jenkins env variables
# may not be set...
if [ -z "$WORKSPACE" ]; then
  WORKSPACE="$PWD"
    INTERACTIVE=1
else
    INTERACTIVE=0
fi

if [ "$INTERACTIVE" == "0" ]; then
  if [ "$WIN32" == "1" ]; then
    WORKSPACE="$PWD"
  fi
fi

if [ -z "$BUILD_NUMBER" ]; then
    BUILD_NUMBER=$(date +%Y_%m_%d_interactive)
fi

#BUILD="$WORKSPACE/${PROJECT}_${BUILD_NUMBER}_linux_build"
BUILD="build"


echo "INFO: BUILD=\"$BUILD\""

if [ -d "$BUILD" ]; then
  echo "WARN: Build directory already exists!"
else
    mkdir -p "$BUILD"
fi

pushd "$BUILD"

if [ -z "$SKIP_CHECKOUT" ]; then

    echo "INFO: Checking out source code"
    
    # Checkout stx
    if [ ! -d stx ]; then
      if ! cvs co stx; then
        echo "ERROR: CVS: Cannot checkout stx";  
        exit;
      fi
    else
      (cd stx && cvs update -d)
    fi
    
    # Checkout stx:libjava
    if [ -d stx/libjava/CVS ]; then
      rm -rf stx/libjava
    fi

    if [ ! -d stx/libjava ]; then
      if ! hg clone https://swing.fit.cvut.cz/hg/stx.libjava/ stx/libjava -b development;  then
        echo "ERROR: HG: Cannot clone stx/libjava";  
        exit;
      fi     
    else 
      if ! hg --cwd stx/libjava pull;  then
        echo "ERROR: HG: Cannot pull stx/libjava";  
        exit;
      fi
      if ! hg --cwd stx/libjava update;  then
        echo "ERROR: HG: Cannot update stx/libjava";  
        exit;
      fi
    fi

else
    echo "INFO: Checkout skipped (SKIP_CHECKOUT=$SKIP_CHECKOUT)"
fi

# Compile

if [ -z "$SKIP_COMPILE" ]; then

    echo "INFO: Compiling"
    TARGETS1="stx stx/libjava stx/goodies/petitparser stx/libjava/tools stx/libjava/experiments stx/projects/smalltalk stx/librun"
    for target in $TARGETS1; do
      if ! (cd "$target" && $MAKE); then
        echo "ERROR:  cannot $MAKE in $target"
        exit 3
      fi
    done
  
else
    echo "INFO: Compilation skipped (SKIP_COMPILE=$SKIP_COMPILE)"
fi

popd

echo "INFO: $PROJECT SUCCESSFULLY BUILT"
