#!/bin/bash
# Info: Script to install/create/delete/update crun from file settings 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-10-2024

USAGE="install-crun.sh install | update | remvoe"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
OS="$(uname | tr '[:upper:]' '[:lower:]')"

[ -r "env-crun" ] && . ./env-crun

CRUN_VERSION="${CRUN_VERSION:-1.5}"
CRUN_URL=https://github.com/containers/crun/releases/download/$CRUN_VERSION/crun-$CRUN_VERSION-$OS-$ARCH

CMD_TSKSRVC=${1:-install}

export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

ORG=$(pwd)

_init() {
  [ -z "$CRUN_VERSION" ] && exit 1  # || [ -z "$CRUN_ARCH" ] || [ -z "$CRUN_URL" ] || [ -z "$CRUN_FILE" ] && exit 1
  local curr_vers
  local has_crun
  has_crun=$(type crun 2>/dev/null)
  if [ -n "$has_crun" ]  ; then 
    curr_vers=$(crun --version | grep "^Version" | awk '{print $2}')
  fi
  if [ "$curr_vers" != "$CRUN_VERSION" ] ; then
    if ! curl -fsSL "$CRUN_URL" -o crun ; then
      echo "error downloading crun "
      return 1 
    fi
    if [ -r "crun" ] ; then
      chmod +x crun
      sudo mv crun /usr/local/bin
    else  
      echo "error installing crun"
      ret=1
    fi
    rm -f crun
    [ "$ret" == 1 ] && return 1 
    [ -r "/usr/bin/crun" ] && mv /usr/bin/crun /usr/bin/_crun
  fi
  return 0 
}

_config_crun() {
  return 0
  [ ! -d "/etc/crun" ] && mkdir -p /etc/crun 
  if [ -r "crun_config.toml" ] && [ ! -r "/etc/crun/config.toml" ] ; then
    sudo cp crun_config.toml /etc/crun/config.toml
  fi
  if [ -r "crictl.yaml" ] && [ ! -r "/etc/crun-crictl.yaml" ] ; then
    sudo cp crictl.yaml /etc/crun-crictl.yaml
  fi
  #if [ -r "crictl.yaml" ] && [ ! -r "/etc/crictl.yaml" ] ; then
  #  sudo cp crictl.yaml /etc/crictl.yaml
  #fi

  if [ -r "crun.service" ] && [ ! -r "/lib/systemd/crun.service" ] ; then
    sudo cp crun.service  /lib/systemd/system
    [ ! -L "/etc/systemd/system/crun.service" ] && sudo ln -s /lib/systemd/system/crun.service /etc/systemd/system
    sudo timeout -k 10 20 systemctl daemon-reload
  fi
  TARGET=/etc/modules-load.d/crun.conf
  ITEMS="overlay br_netfilter"
  for it in $ITEMS
  do 
    has_item=$(sudo grep ^"$it" $TARGET 2>/dev/null)
    [ -z "$has_item" ] && echo "$it" | sudo tee -a /etc/modules-load.d/crun.conf
  done
  _start_crun
}

_remove_crun() {
  sudo timeout -k 10 20 systemctl stop crun 
  sudo timeout -k 10 20 systemctl disable crun 
}

_start_crun() {
  if [ "$CRUN_SYSTEMCTL_MODE" == "enabled" ] ; then 
    sudo timeout -k 10 20 systemctl enable crun 
  else 
    sudo timeout -k 10 20 systemctl disable crun 
  fi
  sudo timeout -k 10 20 systemctl start crun
}

_restart_crun() {
  sudo timeout -k 10 20 systemctl restart crun
}
[ "$CMD_TSKSRVC" == "remove" ] && _remove_crun && exit 0
if ! _init ; then
  echo "error crun install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_crun && exit 0
if ! _config_crun ; then 
  echo "error crun config"
  exit 1
fi
#if ! _start_crun ; then 
#  echo "error crun start"
#  exit 1
#fi
