#!/bin/bash
# Info: Script to install oras
# Author: JesusPerezLorenzo
# Release: 1.0
# Date: 11-01-2024

USAGE="install-oci_reg.sh "
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

ORG=$(dirname "$0")

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

_get_version() {
   local curr_version
   #JQ=$(type -P jq)
   #[ -z "$JQ" ] && echo "jq not installed " && exit 1
   local cmd=$1
   local out
   out="/tmp/oci.$$"
   $cmd -v &>"$out"
   #curr_version=$($JQ < "$out" '.commit' | sed 's/v//g' | sed 's/"//g')
   cur_version=$(cat $out | cut -f3 -d"," | cut -f2 -d":" | sed 's/"//g')
   rm -r "$out"
   echo "$curr_version"
}

_install_oci_reg() {
   [ -z "$VERSION" ] && echo "VERSION not found" && exit  1
   local has_cmd
   local curr_vers
   for cmd in $OCI_CMDS
   do
      has_cmd=$(type "$cmd" 2>/dev/null)
      if [ -n "$has_cmd" ]  ; then
         curr_vers=$(_get_version "$cmd")
      else
         curr_vers=""
      fi
      if [ -n "$curr_vers" ] && [[ "$curr_vers" =~ $VERSION ]] ; then
         continue
      else
         if ! curl -fsSLO "https://github.com/project-zot/zot/releases/download/v${VERSION}/${cmd}-${OS}-${ARCH}"; then
            echo "Error download v${VERSION}/${cmd}-${OS}-${ARCH} "
            exit 1
         fi
         sudo mv "${cmd}-${OS}-${ARCH}" /usr/local/bin/"${cmd}"
         sudo chmod +x /usr/local/bin/"${cmd}"
      fi
   done
}

_config_oci_reg() {
   local has_user
   [ -z "$OCI_USER" ] && echo "OCI_USER not found" && exit  1
   has_user=$(sudo grep "$OCI_USER" /etc/passwd)
   if [ -z "$has_user" ] ; then
      sudo adduser --no-create-home --disabled-password --gecos --disabled-login "$OCI_USER"
   fi
   if [ ! -d "$OCI_DATA" ] ; then
      sudo mkdir -p "$OCI_DATA"
      sudo chown -R "${OCI_USER}:${OCI_USER_GROUP}" "$OCI_DATA"
   fi
   if [ ! -d "$OCI_LOG" ] ; then
      sudo mkdir -p "$OCI_LOG"
      sudo chown -R "${OCI_USER}:${OCI_USER_GROUP}" "$OCI_LOG"
   fi
   [ ! -d "$OCI_ETC" ] && sudo mkdir "$OCI_ETC"
   if [ -r "$ORG/config.json" ] ; then
      sudo cp "$ORG/config.json" "$OCI_ETC"
   fi
   if [ -r "$ORG/htpasswd" ] ; then
      sudo mv "$ORG/htpasswd" "$OCI_ETC"
      sudo chown "$OCI_USER" "$OCI_ETC"/htpasswd
      sudo chmod 400 "$OCI_ETC"/htpasswd
   fi
   [ -d "$OCI_ETC/ssl" ] && sudo rm -rf "$OCI_ETC/ssl"
   if [ -r "$ORG/ssl" ] ; then
      sudo mv "$ORG/ssl" "$OCI_ETC"
      sudo chown -R "$OCI_USER" "$OCI_ETC"/ssl
      sudo chmod 400 "$OCI_ETC"/ssl/privkey.pem
   fi
   if [ -r "$ORG/zot.service" ] ; then
      sudo cp zot.service  /lib/systemd/system
      [ ! -L "/etc/systemd/system/zot.service" ] && sudo ln -s /lib/systemd/system/zot.service /etc/systemd/system
      sudo timeout -k 10 20 systemctl daemon-reload
      sudo timeout -k 10 20 systemctl enable zot 2>/dev/null
   fi
   sudo timeout -k 10 20 systemctl restart zot
   if [ -r "$ORG/zli-cfg" ] ; then
      cp "$ORG/zli-cfg" "$HOME/.zot"
   fi
}

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

# Update and add packages to installation
[ -z "$1" ] || [ "$1" == "install" ] && _install_oci_reg
[ -z "$1" ] || [ "$1" == "config" ] && _config_oci_reg
