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

USAGE="install-runc.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-runc" ] && . ./env-runc

RUNC_VERSION="${RUNC_VERSION:-1.1.13}"
RUNC_URL=https://github.com/opencontainers/runc/releases/download/v$RUNC_VERSION/runc.$ARCH

CMD_TSKSRVC=${1:-install}

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

ORG=$(pwd)

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

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

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

_remove_runc() {
  sudo timeout -k 10 20 systemctl stop runc 
  sudo timeout -k 10 20 systemctl disable runc 
}

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

_restart_runc() {
  sudo timeout -k 10 20 systemctl restart runc
}
[ "$CMD_TSKSRVC" == "remove" ] && _remove_runc && exit 0
if ! _init ; then
  echo "error runc install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_runc && exit 0
if ! _config_runc ; then 
  echo "error runc config"
  exit 1
fi
#if ! _start_runc ; then 
#  echo "error runc start"
#  exit 1
#fi
