#!/bin/sh
#
##############################
## somenet.org ansible tool ##
##############################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2026 by someone <someone@somenet.org>
#
# update git repository
#
# This is always run inside the somesible repo only.
#

# .git may be a file, if checked out as submodule.
if [ ! -e ".git" ]; then
  echo "*** ERROR (update_somesible.sh): this is not a git repo. abort."
  exit 1
fi

SOMESIBLE_GIT_REV_START="$(git --git-dir ./.git --no-pager log --pretty=format:'%h' -n 1 HEAD --)"

echo "***"
echo "*** DEBUG (update.sh) start: [${SOMESIBLE_GIT_REV_START}] $(date -Isec)"
echo "***"

# (try to) read config in parent dir.
if [ -e "../somesible.conf" ]; then
    . "../somesible.conf"
fi
if [ -z "$SOMESIBLE_VERSION" ]; then
    SOMESIBLE_VERSION="tags/activeVersion"
else
    echo "***********************************************************"
    echo "*** WARNING (update_somesible.sh): using non-default version: ${SOMESIBLE_VERSION} ***"
    echo "***********************************************************"
    echo "***"
fi


################################
# update self (somesible repo) #
################################
chmod -R u=rwX,go-rwx "./"
chmod u+x *.sh

git config protocol.ext.allow always
git config remote.origin.url "ext::ssh -F ./.ssh/config git.somenet.org %S /root/pub/somesible"
git config remote.update.url 'git@git.somenet.org:/root/pub/somesible'
git config fetch.prune true

git ls-remote --exit-code -t origin > /dev/null 2>&1
USERCONFIG_OFFLINE=$?
if [ $USERCONFIG_OFFLINE -ne 0 ]; then
    echo "****************************************************************************"
    echo "*** INFO (update.sh): git repo unreachable. Offline? - Retry in 120 sec. ***"
    echo "****************************************************************************"
    echo "***"
    sleep 120
    git ls-remote --exit-code -t origin > /dev/null 2>&1
    USERCONFIG_OFFLINE=$?
fi

if [ $USERCONFIG_OFFLINE -ne 0 ]; then
    echo "***************************************************************************"
    echo "*** WARNING (update.sh): git repo unreachable. Offline? - Retry failed. ***"
    echo "***************************************************************************"
    echo "***"
else
    git tag | xargs -n1 git tag -d
    git fetch origin --tags
fi
unset USERCONFIG_OFFLINE

git checkout -f "master"
git reset --hard "$SOMESIBLE_VERSION" --
if [ $? -ne 0 ]; then
    echo "******************************************************************"
    echo "*** WARNING (update_somesible.sh): invalid SOMESIBLE_VERSION? ***"
    echo "******************************************************************"
    echo "***"
    git reset --hard "tags/activeVersion" --
fi

# one --force is not enought!
git clean -ffdx
git gc --prune=all

# Do this twice to be sure.
chmod -R u=rwX,go-rwx "./"
chmod u+x *.sh


echo "***"
echo "*** DONE (update_somesible.sh) [${SOMESIBLE_GIT_REV_START}] -> [$(git --git-dir ./.git --no-pager log --pretty=format:'%h' -n 1 HEAD --)] $(date -Isec)"
echo "***"

###########
# cleanup #
###########
unset SOMESIBLE_VERSION
unset SOMESIBLE_GIT_REV_START
