#!/bin/bash
SCRIPT=$0
#REPOS=$1
#CMTID=$2

ERR=0
#LOG="/home/git/gitea/data/gitea-repositories/kolibrios/kolibrios.git/hooks/post-receive.log"
#LOG="/home/kolibri/docker/data/gitea/git/repositories/kolibrios/kolibrios.git/hooks/post-receive.log"
LOG="/home/autobuild/git-hooks/post-receive.log"

_log()
{
    echo "[$(/bin/date)] $@" >> "$LOG"
}


#/usr/bin/sudo -u kolibri "$SU_SCRIPT" "/home/autobuild/git-hooks/post-receive.bot" "$@"
REPOS=/home/autobuild/localcopy_git
git -C $REPOS fetch --all
#/usr/bin/sudo -u autobuild git -C $REPOS reset --hard origin/main
git -C $REPOS pull
/usr/bin/sudo -u kolibri "/home/autobuild/git-hooks/post-receive.bot" "$@"
ERR=$?
_log "($SCRIPT) exit code = $ERR"
[ $ERR -eq 0 ] || exit $ERR

source kos32-export-env-vars $REPOS

REPOS=/home/autobuild/localcopy_git
ps xa | grep tup | grep monitor
if [ $? -ne 0 ]; then tup monitor; fi
( flock 9
#export PATH=$PATH:/home/autobuild/tools/bin:/home/autobuild/tools/win32.main/bin
export PATH=/home/autobuild/tools/win32/bin:$PATH:/home/autobuild/tools/bin:/home/autobuild/tools/win32.main/bin
export LANG=en_US.UTF-8
EXITCODE=0
echo `date` ": updating localcopy_git" >> /home/autobuild/localcopy_git/general-log.txt
cd /home/autobuild/localcopy_git
#git pull
#CMTID=`git log -1 --format='%h' --abbrev=7`
CMTID=`git -C $REPOS describe --tags --long --abbrev=7 | sed 's/\-g/\-/' | sed 's/\-/\+/'`
if [ $? -ne 0 ]; then echo "Failed to update repository copy" >&2; exit 13; fi
echo `date` ": successful" >> /home/autobuild/localcopy_git/general-log.txt
echo -e "#!/bin/sh\necho $CMTID" > /home/autobuild/tools/bin/get-current-cmtid
for a in data/*/tup.config
do
  config=$(basename $(dirname $a))
  if [ -d build-$config ]
  then :
  else
    mkdir build-$config
    mkdir /home/kolibri/sites/org.kolibrios.builds/$config
    ln -s /home/autobuild/localcopy_git/build-$config /home/kolibri/sites/org.kolibrios.builds/$config/data
    echo CONFIG_BUILD_TYPE=$config >> build-$config/tup.config
    echo 'CONFIG_KPACK_CMD=&& kpack --nologo "%o"' >> build-$config/tup.config
    echo 'CONFIG_KERPACK_CMD=&& kerpack %o' >> build-$config/tup.config
    echo 'CONFIG_PESTRIP_CMD=&& EXENAME=%o fasm $(ROOT)/data/common/pestrip.asm %o' >> build-$config/tup.config
    echo 'CONFIG_INSERT_COMMIT_ID=1' >> build-$config/tup.config
    cat $a >> build-$config/tup.config
  fi
done
# KOLIBRIOS_BUILD_CMTID, KOLIBRIOS_BUILD_OFFSET, KOLIBRIOS_BUILD_DBGTAG
#source kos32-export-env-vars $REPOS
tup > "/home/kolibri/sites/org.kolibrios.builds/logs/kolibrios-$CMTID-log.txt" 2> "/home/kolibri/sites/org.kolibrios.builds/logs/kolibrios-$CMTID-err.txt"
) 9>/home/autobuild/git-hooks/.build-lock
ERRORLEVEL=$?
if [ $ERRORLEVEL -eq 13 ]
then
  exit 2
fi

export CMTID
export ERRORLEVEL
nohup /home/autobuild/git-hooks/post-build $ERRORLEVEL >> /home/autobuild/localcopy_git/post-build-out 2>>/home/autobuild/localcopy_git/post-build-err &
if [ $ERRORLEVEL -ne 0 ]
then
  echo "Auto-build failed." >&2
  echo "Examine http://builds.kolibrios.org/logs/kolibrios-$CMTID-err.txt for list of errors." >&2
  USER="`git -C $REPOS log -1 --format='%al'`"
  if [ "$USER" = "dent.ace" ]
  then USER="ace_dent"
  elif [ "$USER" = "dent.ace" ]
  then USER="ace_dent"
  fi

#  LAST_SUCCESS=`cat /home/autobuild/git-hooks/last-success`
#  if [ `expr $REV - $LAST_SUCCESS` -ge 5 ]
#  then
#    PICTURE="yuno.gif"
#  else
#    PICTURE="facepalm.gif"
#  fi
#  if [ `expr $REV - $LAST_SUCCESS` -ge 2 ]
#  then
#    /home/autobuild/status/report.sh epicfail $CMTID $USER $PICTURE
#  else
    /home/autobuild/status/report.sh fail $CMTID $USER
#  fi
  exit $ERRORLEVEL
fi
echo $CMTID > /home/autobuild/git-hooks/last-success
exit 0
