#!/QOpenSys/usr/bin/ksh

set -eu

SUCCESS=0

# Create a temp directory to extract the stage 2 bootstrap in to
TEMPDIR=/tmp/bootstrap.$$
mkdir -p $TEMPDIR
cd $TEMPDIR


function finished
{
  set +eu
  cd /tmp
  rm -f $TEMPDIR/*
  rmdir $TEMPDIR
  
  if [ "$SUCCESS" = '1' ]
  then
    echo 'Bootstrap succeeded at' `date`
  else
    echo 'Bootstrap failed at' `date`
  fi
}
trap finished EXIT

VERSION=`uname -v``uname -r`
if [ 0$VERSION -lt 73 ]
then
  echo "This system is too old!"
  echo "IBM i 7.3 or above is required."
  exit 1
fi

if [ -f /tmp/bootstrap.in-progress-dont-remove ]
then
  echo "Previous bootstrap did not complete, re-running"
  echo "Previous run started" `cat /tmp/bootstrap.in-progress-dont-remove`
  BOOTSTRAPPED=no
elif [ ! -e /QOpenSys/var/lib/rpm ]
then
  BOOTSTRAPPED=no
else
  BOOTSTRAPPED=yes

  if [ $# -gt 0 ]
  then
    # Can't merge this with the previous check due to being possibly unset
    if [ $1 = "--yes-this-is-unsupported-but-i-know-what-i-am-doing" ]
    then
      date > /tmp/bootstrap.info
      BOOTSTRAPPED=no
    fi
  fi
fi

if [ $BOOTSTRAPPED = "yes" ]
then
  cat <<EOF
########     ###    ##    ##  ######   ######## ########
##     ##   ## ##   ###   ## ##    ##  ##       ##     ##
##     ##  ##   ##  ####  ## ##        ##       ##     ##
##     ## ##     ## ## ## ## ##   #### ######   ########
##     ## ######### ##  #### ##    ##  ##       ##   ##
##     ## ##     ## ##   ### ##    ##  ##       ##    ##
########  ##     ## ##    ##  ######   ######## ##     ##

This system has already been bootstrapped!

Bootstrapping is a one-time process and should never need to be done again.
Doing so would cause the installation status of everything installed via yum
to become unknown and any software in the bootstrap will overwrite any newer
versions that may have been installed.

If you want to continue anyway, you can re-run bootstrap.sh with the flag
--yes-this-is-unsupported-but-i-know-what-i-am-doing
EOF
  exit 1
fi

# Create a boostrap in-progress marker so we can detect failed bootstraps
# and re-run automatically without requiring manual intervention
date > /tmp/bootstrap.in-progress-dont-remove
echo "Starting bootstrap at" `cat /tmp/bootstrap.in-progress-dont-remove`
echo "Please do not interrupt"

# NOTE: the .tar.Z is no longer a compressed GNU tar file, but instead an
# uncompressed PASE tar file containing the stage 2 compresed GNU tar file and
# binaries needed to extract it. The .tar.Z name is kept for compatibility.
echo "Extracting second stage bootstrap"
/QOpenSys/usr/bin/tar -xof /tmp/bootstrap.tar.Z

# Change to the root directory
cd /

echo "Running second stage bootstrap"
# Extract the bootstrap in to the root
# NOTE: checkpoint every 5120 records gives a totals output every 50M
$TEMPDIR/zstd.bin -dc $TEMPDIR/bootstrap-stage2.tar.zst | \
    $TEMPDIR/tar.bin \
    --same-permissions \
    --same-owner \
    --checkpoint=5120 \
    --checkpoint-action=totals \
    -xf -

# Manually link /bin/bash. If we put it in the bootstrap, it overwrites
# the /bin -> /usr/bin symlink :(
ln -sf /QOpenSys/pkgs/bin/bash /bin/bash

# We succeeded! Remove our in-progress marker
rm /tmp/bootstrap.in-progress-dont-remove
SUCCESS=1
exit 0
