#!/bin/bash
# Info: Script to install webhook with provisioning 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 19-10-2023

USAGE="install-webhook.sh "

[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

function _create_user() {
    local has_user
    sudo chmod 1777 /tmp
    [ -z "${WEBHOOK_USER}" ] && return
    has_user=$(sudo grep "${WEBHOOK_USER}" /etc/passwd)
    if [ -z "$has_user" ] ; then
      sudo adduser \
      --system \
      --shell "/bin/bash" \
      --gecos "$WEBHOOK_USER user" \
      --group \
      --disabled-password \
      --home "$WEBHOOK_HOME" \
      "${WEBHOOK_USER}"
    else
      echo "User $WEBHOOK_USER already exists"
      return 
    fi
    [ ! -d "$WEBHOOK_HOME" ]  && sudo mkdir -p "$WEBHOOK_HOME"
    if [ -d "$SOURCE_USER_PATH" ] && [ -r "$SOURCE_USER_PATH/.profile" ] && [ -n "$WEBHOOK_HOME" ] ; then 
      if [ -z "$(sudo ls "$WEBHOOK_HOME"/.profile 2>/dev/null)" ] ; then
        [ -r "$SOURCE_USER_PATH/.profile" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.profile "$WEBHOOK_HOME"
      fi
      if [ -z "$(sudo ls "$WEBHOOK_HOME"/.bashrc 2>/dev/null)" ] ; then
        [ -r "$SOURCE_USER_PATH/.bashrc" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.bashrc "$WEBHOOK_HOME"
      fi
      if [ -z "$(sudo ls "$WEBHOOK_HOME"/.bash_aliases 2>/dev/null)" ] ; then
        [ -r "$SOURCE_USER_PATH/.bash_aliases" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.bash_aliases "$WEBHOOK_HOME"
      fi
      if [ -z "$(sudo ls "$WEBHOOK_HOME"/.ssh 2>/dev/null)" ] && [ -r "$SOURCE_USER_PATH/.ssh" ] ; then
        sudo cp -pvr "$SOURCE_USER_PATH"/.ssh "$WEBHOOK_HOME"
        [ -r "/home/$INSTALLER_USER/.ssh/authorized_keys" ] && cat "/home/$INSTALLER_USER/.ssh/authorized_keys" | sudo tee -a "$WEBHOOK_HOME/.ssh/authorized_keys"> /dev/null
      elif [ ! -d "$WEBHOOK_HOME/.ssh" ] ; then
        sudo mkdir -p "$WEBHOOK_HOME/.ssh" 
        [ -r "/home/$INSTALLER_USER/.ssh/authorized_keys" ] && cat "/home/$INSTALLER_USER/.ssh/authorized_keys" | sudo tee -a "$WEBHOOK_HOME/.ssh/authorized_keys"> /dev/null
      fi
      sudo cp -pr "$SOURCE_USER_PATH"/* "$WEBHOOK_HOME"
      sudo chown -R "$WEBHOOK_USER":"$WEBHOOK_USER_GROUP" "$WEBHOOK_HOME"
    fi
    if [ ! -r "/etc/sudoers.d/$WEBHOOK_USER" ] ; then
      echo "$WEBHOOK_USER  ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers.d/"$WEBHOOK_USER"
    fi
    sudo rm -r "$SOURCE_USER_PATH"
}
function _download_webhook {
  local has_webhook
  local webhook_version
  local num_version
  local expected_version_num
  OS="$(uname | tr '[:upper:]' '[:lower:]')" 
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" 

  if [ -n "$WEBHOOK_VERSION" ]  ; then 
    has_webhook=$(type -P webhook)
    num_version=""
    [ -n "$has_webhook" ] && webhook_version=$(webhook -version | cut -f3 -d" ") && num_version=${webhook_version//\./}
    expected_version_num=${WEBHOOK_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ -z "$num_version" ] || [ "$num_version" -lt "$expected_version_num" ] ; then 
      curl -fsSLO "https://github.com/adnanh/webhook/releases/download/$WEBHOOK_VERSION/webhook-${OS}-${ARCH}.tar.gz"
        tar xzf "webhook-${OS}-${ARCH}.tar.gz" &&
        sudo mv "webhook-${OS}-${ARCH}/webhook" /usr/local/bin/webhook &&
        rm -rf "webhook-${OS}-${ARCH}.tar.gz" "webhook-${OS}-${ARCH}" &&
        echo "webhook installed "
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "webhook" "$webhook_version" "expected $WEBHOOK_VERSION"
    else
      printf "%s\t%s\n" "webhook" "already $WEBHOOK_VERSION"
    fi
  fi
}
function _copy_files {
  [ ! -r "hooks.conf" ] && echo "No hooks.conf found to create service" && exit 1
  [ ! -d "/etc/webhook" ] && sudo  mkdir -p /etc/webhook
  sudo cp hooks.conf /etc/webhook/"$WEBHOOK_CONF"
  [ -r ".scrt" ] && sudo cp .scrt /etc/webhook
  sudo chown -R "$WEBHOOK_USER":"$WEBHOOK_USER_GROUP" /etc/webhook
  [ -n "$WEBHOOK_LOG_PATH" ] && [ ! -r "$WEBHOOK_LOG_PATH" ] && sudo touch "$WEBHOOK_LOG_PATH" && sudo chown "$WEBHOOK_USER":"$WEBHOOK_USER_GROUP"  "$WEBHOOK_LOG_PATH"
  if [ -n "$REPO_USERNAME" ] ; then 
    local repo_user_home 
    repo_user_home=$(grep "^$REPO_USERNAME" /etc/passwd | cut -f6 -d":")
    if [ -d "$repo_user_home/.profile" ] ; then 
      [ -d "$repo_user_home" ] && [ -r "ssh_config" ] && sudo cp ssh_config "$repo_user_home"/.ssh/config && sudo chown "$REPO_USERNAME" "$repo_user_home"/.ssh/config
      if [  -n "$REPO_SSH_KEY" ] && [ -d  ".ssh" ] && [ ! -r "$repo_user_home/.ssh/$(basename "$REPO_SSH_KEY").pub" ] ;then
        sudo cp .ssh/* "$repo_user_home/.ssh" 
        sudo chown "$REPO_USERNAME" "$repo_user_home"/.ssh/*
      fi
    fi
  fi
  [ -r "on_webhook_provisioning" ] && sudo cp on_webhook_provisioning /usr/local/bin
} 
function _create_service {
  [ ! -r "webhook.service" ] && echo "No webhook.service found to create service" && exit 1
  #[ -r "/lib/systemd/system/webhook.service" ]  && return 
  sudo cp webhook.service /lib/systemd/system/webhook.service >/dev/null 2>&1
  sudo systemctl daemon-reload >/dev/null 2>&1
  sudo systemctl enable webhook.service >/dev/null 2>&1
  sudo systemctl restart webhook.service >/dev/null 2>&1
}

[ -r "./env-webhook"  ] && . ./env-webhook
_create_user
_download_webhook
_copy_files
_create_service
