#!/bin/bash
# Info: Script to install Gitea
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-12-2023

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

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

GITEA_VERSION=${GITEA_VERSION:-1.21.7} 

GITEA_URL=https://dl.gitea.io/gitea
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
GITEA_FILE=$GITEA_VERSION/gitea-$GITEA_VERSION-linux-$ARCH
GITEA_ARCH=linux-$ARCH

GITEA_RUN_PATH=${GITEA_RUN_PATH:-/usr/local/bin/gitea}

GITEA_SYSTEMCTL_MODE=${GITEA_SYSTEMCTL_MODE:-enabled}

GITEA_ETC_PATH=${GITEA_ETC_PATH:-/etc/gitea}

GITEA_LIB_PATH=${GITEA_LIB_PATH:-/home2/lib/gitea}
GITEA_CONFIG_FILE=${GITEA_CONFIG_FILE:-app.ini}

GITEA_RUN_USER=${GITEA_RUN_USER:-gitea}
GITEA_RUN_GROUP=${GITEA_RUN_GROUP:-gitea}
GITEA_RUN_USER_HOME=${GITEA_RUN_USER_HOME:-/home/gitea}

GITEA_PKG_NAME=gitea

CMD_TSKSRVC=${1:-install}

#ORG="$(pwd)"
export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

_init() {
  local curr_vers
	[ -z "$GITEA_VERSION" ] || [ -z "$GITEA_ARCH" ] || [ -z "$GITEA_URL" ] || [ -z "$GITEA_FILE" ] && exit 1
  if [ -x "$GITEA_RUN_PATH" ] ; then 
    curr_vers=$(${GITEA_RUN_PATH} -v | awk '{print $3}')
  else 
    curr_vers=0
  fi
  if [ "$curr_vers" != "$GITEA_VERSION" ] || [ "$curr_vers" != "$GITEA_VERSION" ]; then
  if curl -fsSL "${GITEA_URL}/${GITEA_VERSION}/gitea-${GITEA_VERSION}-${GITEA_ARCH}" -o gitea ; then 
    sudo mv gitea "${GITEA_RUN_PATH}"
    sudo chmod +x "${GITEA_RUN_PATH}"
  else 
      echo "error download ${GITEA_URL}/${GITEA_VERSION}/gitea-${GITEA_VERSION}-${GITEA_ARCH}"
    return 1
  fi
  fi 
  return 0
}

_config_gitea() {
  local has_user=""
  local http_addr
  local etc_entry
  has_user=$(grep "$GITEA_RUN_USER" /etc/passwd)
  if [ -z "$has_user" ] ; then 
		sudo adduser \
			--system \
			--shell /bin/bash \
			--gecos 'Git Version Control' \
			--group \
			--disabled-password \
			--home "$GITEA_RUN_USER_HOME" \
			"${GITEA_RUN_USER}"
  fi
	if [ ! -d "$GITEA_RUN_USER_HOME" ] ; then 
    sudo mkdir -p "$GITEA_RUN_USER_HOME"
		sudo chown -R "$GITEA_RUN_USER":"$GITEA_RUN_GROUP" "$GITEA_RUN_USER_HOME"
	fi
  sudo mkdir -p "${GITEA_LIB_PATH}"/{custom,data,log}
	sudo chown -R "${GITEA_RUN_USER}:${GITEA_RUN_GROUP}" "${GITEA_LIB_PATH}"
	sudo chmod -R 750 "${GITEA_LIB_PATH}"
	[ ! -d "${GITEA_ETC_PATH}" ] && sudo mkdir "${GITEA_ETC_PATH}"
  sudo chmod 750 "${GITEA_ETC_PATH}"
	sudo chown -R root:"${GITEA_RUN_GROUP}" "${GITEA_ETC_PATH}"

  [ ! -r "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}" ] && [ -r "app.ini" ] && sudo cp app.ini  "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}"
  sudo chown "$GITEA_RUN_USER":"$GITEA_RUN_GROUP" "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}"
	[ -r "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}" ] && sudo chmod 640 "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}"

  if [ ! -r "${GITEA_ETC_PATH}/.psql.sql" ] && [ -r "psql.sql" ] ; then 
      sudo cp psql.sql  "${GITEA_ETC_PATH}/.psql.sql"
      case "$GITEA_DB_TYPE" in 
        postgres)  sudo -u postgres psql < psql.sql
        ;;
      esac
      rm -f psql.sql
      sudo chmod 400 "${GITEA_ETC_PATH}/.psql.sql"
  fi
  if [ -d "ssl" ] ; then 
    sudo rm -rf "${GITEA_SSL_CERTS_PATH}"
    sudo cp -pr ssl "${GITEA_SSL_CERTS_PATH}"
    sudo chown -R "${GITEA_RUN_USER}:${GITEA_RUN_GROUP}" "${GITEA_SSL_CERTS_PATH}"
    sudo chmod 400 "${GITEA_SSL_CERTS_PATH}"/*key*pem 2>/dev/null
  fi
	if [ -r "${GITEA_RUN_PATH}" ] && [ -r "gitea.service" ] ;  then 
    sudo cp gitea.service /lib/systemd/system/gitea.service
    [ "${GITEA_SYSTEMCTL_MODE}" == "enabled" ] && sudo systemctl enable gitea --now >/dev/null 2>&1
    # [ "${GITEA_SYSTEMCTL_MODE}" == "start" ] && sudo systemctl start gitea >/dev/null 2>&1
  fi
  if [ -d "${GITEA_CDCI_USER_HOME}" ] && [ -n "${GITEA_CDCI_USER_HOME}" ] && [ -r "ssh-config" ] ; then 
    sudo cp ssh-config "${GITEA_CDCI_USER_HOME}/.ssh/config"
    if [ -d ".ssh" ] ; then 
      sudo cp -pr .ssh/* "${GITEA_CDCI_USER_HOME}/.ssh"
      sudo chown -R "${GITEA_CDCI_USER}:${GITEA_CDCI_GROUP}" "${GITEA_CDCI_USER_HOME}/.ssh"
    fi
  fi
  [ -d ".ssh" ] && rm -rf .ssh
  http_addr=$(sudo grep HTTP_ADDR /etc/gitea/app.ini | cut -f2 -d"=" | sed "s/ //g")
  if [ -n "$http_addr" ] && [ -n "$GITEA_DOMAIN" ]; then 
    etc_entry=$(sudo grep "$http_addr" /etc/hosts | grep -v "$GITEA_DOMAIN")
    [ -n "$etc_entry" ] && sudo sed -i "s/$etc_entry/$etc_entry $GITEA_DOMAIN/g" /etc/hosts
  fi
  if [ ! -r "$GITEA_ETC_PATH/.done" ] && [ -n "$GITEA_ADM_USER" ] ; then 
    _start_gitea
    echo "wait 11 to gitea init ..."
    sleep 11
    if sudo -u "$GITEA_RUN_USER" gitea admin user create --username "$GITEA_ADM_USER" --password "$GITEA_ADM_PASSWORD" --email "$GITEA_ADM_EMAIL" --admin  --config "${GITEA_ETC_PATH}/${GITEA_CONFIG_FILE}" ; then 
      date +%Y_%m_%d_%H_%M_%S | sudo tee "$GITEA_ETC_PATH/.done"
    fi
  fi
}

_remove_gitea() {
  sudo timeout -k 10 20 systemctl stop "$GITEA_PKG_NAME"  >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable "$GITEA_PKG_NAME" >/dev/null 2>&1
  sudo rm -f "${GITEA_RUN_PATH}"
}

_start_gitea() {
  if [ "$GITEA_SYSTEMCTL_MODE" == "enabled" ] ; then 
    sudo timeout -k 10 20 systemctl enable "$GITEA_PKG_NAME" >/dev/null 2>&1
  else 
    sudo timeout -k 10 20 systemctl disable "$GITEA_PKG_NAME" >/dev/null 2>&1
  fi
  [ -r "/lib/systemd/system/gitea.service" ] && _restart_gitea && return 
  sudo timeout -k 10 20 systemctl start "$GITEA_PKG_NAME" >/dev/null 2>&1
}
_restart_gitea() {
  sudo timeout -k 10 20 systemctl restart "$GITEA_PKG_NAME" >/dev/null 2>&1
}

if [ "$CMD_TSKSRVC" == "remove" ] ; then
  _remove_gitea
  exit
fi
if ! _init ; then
  echo "error gitea install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_gitea && exit 0
if ! _config_gitea ; then 
  echo "error gitea config"
  exit 1
fi
if ! _start_gitea ; then 
  echo "error gitea start"
  exit 1
fi
