#!/bin/bash

# Install and initialize gitea


# Variables 

WKDIR="/srv/forge"

USER="git"
GROUP="gitservice"
GECOS="Gitea User"
USERSHELL="/bin/bash"
HOMEDIR="${WKDIR}/${USER}"

OS="linux"
ARCH="amd64"

APPVERSION="1.10.0"
APPNAME="gitea"
APPBINARY="gitea"
APPSIG="7C9E68152594688862D62AF62D9AE806EC1592E2"
APPASC="${APPNAME}-${APPVERSION}-${OS}-${ARCH}.asc"
KEYSERV="hkp://p80.pool.sks-keyservers.net:80"
DLSERV="dl.gitea.io"
APPDIR="${WKDIR}/gitea"
APPBINDIR="${APPDIR}/bin"
APPCONFDIR="/etc/${APPNAME}"

function configure_proxy {
   proxy=$(CreoleGet activer_proxy_client non)
   if [[ ${proxy} == "oui" ]]
   then
      prAddr=$(CreoleGet proxy_client_adresse)
      prPort=$(CreoleGet proxy_client_port)
      export HTTP_PROXY=http://${prAddr}:${prPort}
      export http_proxy=http://${prAddr}:${prPort}
      export HTTPS_PROXY=http://${prAddr}:${prPort}
      export https_proxy=http://${prAddr}:${prPort}
   fi
}

function create_app_dir {
   
   mkdir -p "${APPDIR}"
   chown ${USER}:${GROUP} "${APPDIR}" -R

   # Create app structure
   mkdir -p ${APPDIR}/{custom,data,indexers,public,log}
   chown ${USER}:${GROUP} ${APPDIR}/{data,indexers,log}
   chmod 750 ${APPDIR}/{data,indexers,log}
   mkdir -p "${APPCONFDIR}"
   chown root:${GROUP} ${APPCONFDIR}
   chmod 770 "${APPCONFDIR}"

   mkdir -p "${APPBINDIR}"
   chown ${USER}:${GROUP} "${APPBINDIR}"

}

function create_user_and_group {
   local existing_group=$(getent group ${GROUP})
   if [ -z "${existing_group}" ]; then
      groupadd ${GROUP}
   fi

   local existing_user=$(getent passwd ${USER})
   if [ -z "${existing_user}" ]; then
      adduser \
         --shell ${USERSHELL} \
         --gecos "${GECOS}" \
         --ingroup ${GROUP} \
         --disabled-password \
         --home ${HOMEDIR} \
         ${USER}
   fi
}

function download_binary {
   local temp_bin=$(mktemp)

   wget -O "${temp_bin}" https://${DLSERV}/${APPNAME}/${APPVERSION}/${APPNAME}-${APPVERSION}-${OS}-${ARCH}
   if [[ $? -ne  0 ]]
   then
      echo "Error downloading binary for ${APPNAME}"
      return 123
   fi

   wget -O "${temp_bin}.asc" https://${DLSERV}/${APPNAME}/${APPVERSION}/${APPASC}
   gpg --keyserver ${KEYSERV} --recv ${APPSIG}
   gpg --verify "${temp_bin}.asc" "${temp_bin}"

   if [[ $? -ne  0 ]]
   then
      echo "Error downloading binary for ${APPNAME}"
      return 123
   fi

   cd $APPBINDIR
   chmod +x "${temp_bin}"
   mv -f "${temp_bin}" ${APPBINARY}
   cd -
}

function current_gitea_version {
   if [[ ! -f "${APPBINDIR}/${APPBINARY}" ]]
   then
      return
   fi
   "${APPBINDIR}/${APPBINARY}" --version | cut -d' ' -f 3
}

function main {
   
   # Check that installed version if ok
   local current_version=$(current_gitea_version)
   if [ "$current_version" == "$APPVERSION" ]; then
      exit 0
   fi
   
   if [ -z "$current_version" ]
   then
      echo "Installing Gitea ${APPVERSION}..."
   else
      echo "Upgrade Gitea from ${current_version} to ${APPVERSION}..."
   fi
   
   configure_proxy
   create_user_and_group
   create_app_dir
   download_binary
   
}

main