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

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

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

CRIO_VERSION="${CRIO_VERSION:-1.29.1}"
#CRIO_URL=https://raw.githubusercontent.com/cri-o/cri-o/master/scripts/get
CRIO_URL=https://storage.googleapis.com/cri-o/artifacts/cri-o.$ARCH.v$CRIO_VERSION.tar.gz

CRICTL_VERSION="${CRICTL_VERSION:-1.29.0}"
CRICTL_URL="https://github.com/kubernetes-sigs/cri-tools/releases/download"

CRIO_SYSTEMCTL_MODE=enabled

CMD_TSKSRVC=${1:-install}

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

ORG=$(pwd)

PKG_ORG=${PKG_ORG:-.}

_clean_others() {
  [ -d "/etc/cni" ] && sudo rm -r /etc/cni
  [ -d "/var/lib/containers" ] && sudo rm -r /var/lib/containers
  sudo rm -f /etc/systemd/system/podman* 2>/dev/null
}
_init() {
  [ -z "$CRIO_VERSION" ] || [ -z "$ARCH" ] || [ -z "$CRIO_URL" ] && exit 1
  local curr_vers
  local has_crio
  has_crio=$(type crio 2>/dev/null)
  if [ -n "$has_crio" ]  ; then 
    curr_vers=$(crio --version | grep "^Version" | awk '{print $2}')
  else
    _clean_others
  fi
  if [ "$curr_vers" != "$CRIO_VERSION" ] ; then
    if ! curl -fsSL "$CRIO_URL" -o /tmp/crio.tar.gz ; then
      echo "error downloading crio r"
      return 1 
    fi
    tar xzf /tmp/crio.tar.gz 
    if [ -r "cri-o/install" ] ; then
      cd cri-o || exit 1
      [ -n "$has_crio" ] && sudo timeout -k 10 20 systemctl stop crio 
      sudo bash ./install &>/dev/null
      cd "$ORG" || exit 1
    else  
      echo "error installing crio"
      ret=1
    fi
    rm -fr cri-o
    rm -f /tmp/crio_installer.sh
    [ "$ret" == 1 ] && return 1 
  fi
  curr_vers=$(crictl --version | awk '{print $3}' | sed 's/v//g')
  if [ "$curr_vers" != "$CRICTL_VERSION" ] ; then
    if ! curl -fsSL "${CRICTL_URL}/v${CRICTL_VERSION}/crictl-v${CRICTL_VERSION}-${OS}-${ARCH}.tar.gz" -o /tmp/crictl.tar.gz ; then
      echo "error downloading crictl installer"
      return 1 
    fi
    tar xzf /tmp/crictl.tar.gz 
    if [ -r "crictl" ] ; then
      chmod +x crictl 
      sudo mv crictl /usr/local/bin
    fi
    rm -f /tmp/crictl.tar.gz 
  fi
  return 0 
}

_config_crio() {
  [ ! -d "/etc/crio" ] && mkdir -p /etc/crio 
  if [ -r "$PKG_ORG/crio_config.toml" ] && [ ! -r "/etc/crio/config.toml" ] ; then
    sudo cp "$PKG_ORG"/crio_config.toml /etc/crio/config.toml
  fi
  if [ -r "$PKG_ORG/crictl.yaml" ] && [ ! -r "/etc/crictl.yaml" ] ; then
    sudo cp "$PKG_ORG"/crictl.yaml /etc/crictl.yaml
  fi

  if [ -r "$PKG_ORG/crio.service" ] && [ ! -r "/lib/systemd/crio.service" ] ; then
    sudo cp "$PKG_ORG"/crio.service  /lib/systemd/system
    [ ! -L "/etc/systemd/system/crio.service" ] && sudo ln -s /lib/systemd/system/crio.service /etc/systemd/system
    sudo timeout -k 10 20 systemctl daemon-reload
  fi
  TARGET=/etc/modules-load.d/crio.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/crio.conf
  done
  [ ! -d "/etc/containers" ] && sudo mkdir /etc/containers
  [ -r "$PKG_ORG/registries.conf" ] && sudo cp "$PKG_ORG"/registries.conf  /etc/containers
  _start_crio
}

_remove_crio() {
  sudo timeout -k 10 20 systemctl stop crio 
  sudo timeout -k 10 20 systemctl disable crio 
}

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

_restart_crio() {
  sudo timeout -k 10 20 systemctl restart crio
}
[ "$CMD_TSKSRVC" == "remove" ] && _remove_crio && exit 0
if ! _init ; then
  echo "error crio install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_crio && exit 0
if ! _config_crio ; then 
  echo "error crio config"
  exit 1
fi
if ! _start_crio ; then 
  echo "error crio start"
  exit 1
fi
