#!/usr/bin/bash
[ ${BASH_VERSION%%.*} -ge 4 ] || exit 1

LANG=en_US.UTF-8
export LANG
SCRIPT=`readlink -f $0`
WHERE=${SCRIPT%/*}

usage() {
  echo "usage: $0 [--(help|cleanup|debian|ubuntu)] [-save | <what to run>]"
  echo "       $0 --help     # shows this"
  echo "       $0 --cleanup  # delete all container used for clonezilla"
  echo "       $0 --debian   # run clonezilla from debian"
  echo "       $0 --ubuntu   # run clonezilla from ubuntu (default)"
  echo "       $0 /bin/bash  # will not run clonezilla but bash instead"
}

OS=ubuntu
if [[ "$1" == "--"* ]]; then
  PARAM=${1#--}
  shift
  case "${PARAM}" in
    help)
      usage
      exit 0
      ;;
    cleanup)
      for i in debian ubuntu ; do
        podman container exists clz-${i} && ( podman stop clz-${i} && podman rm clz-${i} )
        podman image exists localhost/clz-${i} && podman image rm localhost/clz-${i}
        podman image exists docker.io/${i} && podman image rm docker.io/${i}
      done
      exit 0
      ;;
    debian)
      OS=debian
      ;;
    ubuntu)
      OS=ubuntu
      ;;
  esac
fi


podman image exists localhost/clz-${OS}
if [ $? -ne 0 ]; then
  if [ -f ${WHERE}/../image/clz-${OS}.oci-archive.tar ]; then
    echo -e "\n\nimporting podman container from ${WHERE}/../image/clz-${OS}.oci-archive.tar\n"
    podman load -i ${WHERE}/../image/clz-${OS}.oci-archive.tar
  else
    echo -e "\n\nbuilding podman container localhost/clz-${OS}\n"
    BDATE=$(date +%Y-%m-%dT%H:%M)
    (set -x ; buildah bud -t clz-${OS} --build-arg OS=${OS} --build-arg BDATE=${BDATE} /opt/clonezilla/define )
  fi
fi

podman image exists localhost/clz-${OS}
if [ $? -ne 0 ]; then
  echo "ERROR trying to create localhost/clz-${OS} but it did not work"
  exit 1
fi

# saving image
if [[ "$1" == "-save" ]]; then
  [ -d ${WHERE}/../image ] || mkdir ${WHERE}/../image
  if [ -f ${WHERE}/../image/clz-${OS}.oci-archive.tar ]; then
    echo -e "\nERROR clz-${OS}.oci-archive.tar already exists\n"
    exit 1
  fi
  ( set -x ; podman save --format oci-archive -o ${WHERE}/../image/clz-${OS}.oci-archive.tar localhost/clz-${OS} ) && \
  exit $?
fi

if [ $# -eq 0 ]; then
  RUN=clonezilla
  # hint for mounting something on /mnt/partimag
  mount | grep -q /mnt/partimag
  if [ $? -ne 0 ]; then
    echo "                                                                                "
    echo "before starting clonezilla container you probably want to mount local storage"
    echo "to /mnt/partimag (so hit ctrl-c, mount ...)"
    echo "in the container mounting local storage with the GUI  will FAIL (manual works)"
    echo "you can use cifs, nfs, sshfs inside the container"
    echo ""
    read -n 1 -s -r -p "Press any key to continue"
    echo ""
  fi
fi

for i in tmp partimag ; do
  [ -d /mnt/${i} ] || mkdir /mnt/${i}
done

if [ -S "${SSH_AUTH_SOCK}" ]; then
    SSHAGENT=" -v ${SSH_AUTH_SOCK}:/ssh-agent -e SSH_AUTH_SOCK=/ssh-agent"
fi

echo -e "\n\nstarting localhost/clz-${OS} ${RUN} $@\n"
(set -x ; podman run -it --rm \
  --name clz-${OS} \
  --hostname clz-${OS} \
  --security-opt label=disable \
  --privileged \
  -v /etc/localtime:/etc/localtime:ro \
  ${SSHAGENT} \
  -e TERM=xterm-color \
  -v /dev:/dev \
  -v /mnt/tmp:/mnt/tmp \
  -v /mnt/partimag:/home/partimag \
  localhost/clz-${OS} ${RUN} $@ )
