#!/bin/bash

CURR_YEAR=2018

. buildsys/functions

ProcessArgs $*
Assert LoadConfigs
Assert SetupLogDir
Assert SetupResFile

buildTarGZ ()
{
   tar -cf $1.tar $2
   if [ $? != 0 ]
   then
       return 1
   fi
   gzip $1.tar
   if [ $? != 0 ]
   then
       return 1
   fi
}

buildZip ()
{
   zip -r $1.zip $2
   if [ $? != 0 ]
   then
       return 1
   fi
}

getLauncher ()
{
    LVER=$(grep LAUNCHER_VERSION= $WORKDIR/tmpsrc/osplo/etc/launcher.txt)
    LURL=$(grep -i LAUNCHER_URL $WORKDIR/tmpsrc/osplo/etc/launcher.txt)

    LAUNCHER_VERSION=$(echo ${LVER#L*=})
    LAUNCHER_URL=$(echo ${LURL#L*=})
    LAUNCHER_URL+=${LAUNCHER_VERSION}

    #Download only the launcher zip files from the releases area into
    #a directory named launcher within the ospl-3rd-party-software directory
    wget -qr -l 2 -nd -P launcher -A zip -R html,gif $LAUNCHER_URL
}

#
# Update the copyright with the current year which is set by the configure
# script.  The actual year is hard-coded and exported in the THIS_YEAR
# variable so that builds from versions in previous years will still
# contain the original year even if they are rebuilt in a later year.
#
update_copyright ()
{
    upd_docs=`find $1 -type f -print0 | xargs -0 grep -l "to TO_YEAR"`
    for d in $upd_docs
    do
        sed -i "s/TO_YEAR/${THIS_YEAR}/g" $d   
    done
}



if [ "$CREATE_SOURCE" != "yes" ]
then
   echo "CREATE_SOURCE=SKIPPED" >> $RESFILE
   exit 0
fi

cd ../../..

SetState CreatingSource
ArchiveLogs

if [ "$DEP" != "none" ]
then
   OSPL_HOME=$DEPWORKDIR
   export OSPL_HOME
fi

echo "CREATE_SOURCE=RUNNING" >> $RESFILE
ArchiveLogs

#MPC needs to be in the source of ospli so start by getting the
#correct version
(
    cd $OSPL_HOME
    git submodule init
    if [ $? != 0 ]
    then
        exit 1
    fi
    git submodule update
    if [ $? != 0 ]
    then
        exit 1
    fi
) || exit 1

#the etc/license.lic has been added to the .gitattributes so it ignores the file
#during an archive
(
    #Do the tar and zip as the arch is not important its just source
    if [ "$DEP" != "none" ]
    then
        CURRENT_WD="`pwd`"
        OSPLO_SRC=$WORKDIR/tmpsrc/osplo/
        mkdir -p $OSPLO_SRC
        if [ $? != 0 ]
        then
            exit 1
        fi
        git archive --format=tar --prefix=$OSPLO_SRC HEAD | tar -P -xf -
        if [ $? != 0 ]
       then
            exit 1
        fi
        cp $OSPL_HOME/etc/BUILDINFO $OSPLO_SRC/etc/.
        if [ $? != 0 ]
        then
            exit 1
        fi

        update_copyright $OSPLO_SRC

        cd $WORKDIR/tmpsrc
        buildTarGZ $LOGDIR/VortexOpenSplice_src_osplo osplo > /dev/null 2>&1
        if [ $? != 0 ]
        then
            exit 1
        fi
        buildZip $LOGDIR/VortexOpenSplice_src_osplo osplo > /dev/null 2>&1
        if [ $? != 0 ]
        then
            exit 1
        fi
        cd $OSPL_HOME
    fi

    OSPLI_SRC=$WORKDIR/tmpsrc/ospli/
    mkdir -p $OSPLI_SRC
    if [ $? != 0 ]
    then
        exit 1
    fi
    git archive --format=tar --prefix=$OSPLI_SRC HEAD | tar -P -xf -
    if [ $? != 0 ]
    then
        exit 1
    fi
    cp $OSPL_HOME/etc/BUILDINFO $OSPLI_SRC/etc/.
    if [ $? != 0 ]
    then
        exit 1
    fi

    cd $OSPL_HOME/src/tools/protobuf/gpb/
    git archive --format=tar --prefix=$OSPLI_SRC/src/tools/protobuf/gpb/ HEAD | tar -P -xf -
    if [ $? != 0 ]
    then
        exit 1
    fi
    cd -

    #MPC_ROOT needs to be added as the archive above ignores it
    cd submods/MPC_ROOT/
    git archive --format=tar --prefix=$OSPLI_SRC/submods/MPC_ROOT/ HEAD | tar -P -xf -
    if [ $? != 0 ]
    then
        exit 1
    fi

    update_copyright $OSPLI_SRC

    cd $WORKDIR/tmpsrc
    buildTarGZ $LOGDIR/VortexOpenSplice_src_ospli ospli > /dev/null 2>&1
    if [ $? != 0 ]
    then
        exit 1
    fi
    buildZip $LOGDIR/VortexOpenSplice_src_ospli ospli > /dev/null 2>&1
    if [ $? != 0 ]
    then
        exit 1
    fi
    cd -
) || exit 1

(
    if [ -n "$INCLUDE_3RD_PARTY_SOFTWARE" ]
    then
        cd $WORKDIR
        git clone -n $INCLUDE_3RD_PARTY_SOFTWARE
        if [ $? != 0 ]
        then
            exit 1
        fi
        cd ospl-3rd-party-software

        getLauncher

        git archive --format=tar --prefix=ospl-3rd-party-software/ --output=$LOGDIR/ospl-3rd-party-software.tar HEAD
        if [ $? != 0 ]
        then
            exit 1
        fi

        tar --append --file=$LOGDIR/ospl-3rd-party-software.tar launcher/*

        cd ..
        gzip -9 $LOGDIR/ospl-3rd-party-software.tar
        if [ $? != 0 ]
        then
            exit 1
        fi
        if [ -f "$LOGDIR/ospl-3rd-party-software.tar" ]
        then
            rm $LOGDIR/ospl-3rd-party-software.tar
        fi
        cd -

        git archive -9 --format=zip --prefix=ospl-3rd-party-software/ --output=$LOGDIR/ospl-3rd-party-software.zip HEAD
        if [ $? != 0 ]
        then
            exit 1
        fi
        zip -r $LOGDIR/ospl-3rd-party-software.zip launcher/
    fi
) || exit 1

exit 0
