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

USAGE="install-youki.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/')"
ARCH="$(uname -m | sed -e 's/amd64/x86_64/')"
OS="$(uname | tr '[:upper:]' '[:lower:]')"

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

YOUKI_VERSION="${YOUKI_VERSION:-1.7.18}"
YOUKI_URL=https://github.com/containers/youki/releases/download/v$YOUKI_VERSION/youki-$YOUKI_VERSION-$ARCH-gnu.tar.gz

CMD_TSKSRVC=${1:-install}

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

ORG=$(pwd)

_init() {
  [ -z "$YOUKI_VERSION" ] && exit 1  # || [ -z "$YOUKI_ARCH" ] || [ -z "$YOUKI_URL" ] || [ -z "$YOUKI_FILE" ] && exit 1
  local curr_vers
  local has_youki
  has_youki=$(type youki 2>/dev/null)
  if [ -n "$has_youki" ]  ; then 
    curr_vers=$(youki --version | grep "^Version" | awk '{print $2}')
  fi
  if [ "$curr_vers" != "$YOUKI_VERSION" ] ; then
    if ! curl -fsSL "$YOUKI_URL" -o /tmp/youki.tar.gz ; then
      echo "error downloading youki "
      return 1 
    fi
    tar xzf /tmp/youki.tar.gz youki
    if [ -r "youki" ] ; then
      chmod +x youki
      sudo mv youki /usr/local/bin
    else  
      echo "error installing youki"
      ret=1
    fi
    rm -f youki
    rm -f /tmp/youki_installer.sh
    [ "$ret" == 1 ] && return 1 
  fi
  return 0 
}

_config_youki() {
  if [ -r "/etc/containerd/config.toml" ] ; then
    local has_youki=$(grep youki /etc/containerd/config.toml)
    if [ -z "$has_youki" ] ; then
      echo '[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki]' >> /etc/containerd/config.toml
      echo '  runtime_type = "io.containerd.runc.v2"' >> /etc/containerd/config.toml
      echo '   [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki.options]' >> /etc/containerd/config.toml
      echo '     BinaryName = "/usr/local/bin/youki"' >> /etc/containerd/config.toml
      sed -i 's,SystemdCgroup = true,,' /etc/containerd/config.toml
    fi
  fi
  return 0
  #[ ! -d "/etc/youki" ] && mkdir -p /etc/youki 
  #if [ -r "youki_config.toml" ] && [ ! -r "/etc/youki/config.toml" ] ; then
  #  sudo cp youki_config.toml /etc/youki/config.toml
  #fi
  #if [ -r "crictl.yaml" ] && [ ! -r "/etc/youki-crictl.yaml" ] ; then
  #  sudo cp crictl.yaml /etc/youki-crictl.yaml
  #fi
  #if [ -r "crictl.yaml" ] && [ ! -r "/etc/crictl.yaml" ] ; then
  #  sudo cp crictl.yaml /etc/crictl.yaml
  #fi
  #if [ -r "youki.service" ] && [ ! -r "/lib/systemd/youki.service" ] ; then
  #  sudo cp youki.service  /lib/systemd/system
  #  [ ! -L "/etc/systemd/system/youki.service" ] && sudo ln -s /lib/systemd/system/youki.service /etc/systemd/system
  #  sudo timeout -k 10 20 systemctl daemon-reload
  #fi
  #TARGET=/etc/modules-load.d/youki.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/youki.conf
  #done
  #_start_youki
}

_remove_youki() {
  sudo timeout -k 10 20 systemctl stop youki 
  sudo timeout -k 10 20 systemctl disable youki 
}

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

_restart_youki() {
  sudo timeout -k 10 20 systemctl restart youki
}
[ "$CMD_TSKSRVC" == "remove" ] && _remove_youki && exit 0
if ! _init ; then
  echo "error youki install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_youki && exit 0
if ! _config_youki ; then 
  echo "error youki config"
  exit 1
fi
#if ! _start_youki ; then 
#  echo "error youki start"
#  exit 1
#fi
