#!/bin/bash
POSTGRES_VERSION=${POSTGRES_VERSION:-1.16}
POSTGRES_URL="apt https://github.com/postgres/postgres/releases"
POSTGRES_FILE=.
POSTGRES_ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
POSTGRES_OS="$(uname | tr '[:upper:]' '[:lower:]')"

POSTGRES_RUN_PATH=${POSTGRES_RUN_PATH:-/usr/bin/psql}

POSTGRES_SYSTEMCTL_MODE=enabled

POSTGRES_ETC_PATH=${POSTGRES_ETC_PATH:-/etc/postgresql}
POSTGRES_LIB_PATH=${POSTGRES_LIB_PATH:-/var/lib/postgresql}
POSTGRES_DATA_PATH=${POSTGRES_DATA_PATH:-/var/lib/postgresql/$(echo "$POSTGRES_VERSION" | cut -f2 -d".")/main}
POSTGRES_CONFIG_FILE=${POSTGRES_CONFIG_FILE=postgresql.conf}

POSTGRES_RUN_USER=${POSTGRES_RUN_USER:-postgres}
POSTGRES_RUN_GROUP=${POSTGRES_RUN_GROUP:-postgres}
POSTGRES_RUN_USER_HOME=${POSTGRES_RUN_USER_HOME:-/var/lib/postgresql}

POSTGRES_PKG_NAME=postgresql

[ -r "global.sh" ] && . ./global.sh
[ -r "env-postgres" ] && . ./env-postgres

CMD_TSKSRVC=${1:-install}

ORG="$PWD"
export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

_init() {
   #[ -z "$POSTGRES_VERSION" ] || [ -z "$POSTGRES_ARCH" ] || [ -z "$POSTGRES_URL" ] || [ -z "$POSTGRES_FILE" ] && exit 1
   [ -z "$POSTGRES_VERSION" ] && exit 1
   [ -x "$POSTGRES_RUN_PATH" ] && curr_vers=$(${POSTGRES_RUN_PATH} -V | awk '{print $3}')
   if [ "$curr_vers" != "$POSTGRES_VERSION" ] || [ "$curr_vers" != "$POSTGRES_VERSION" ]; then
    #local codename=$(grep VERSION_CODENAME /etc/os-release | cut -f2 -d"=" ) 
    #if [ "$codename"  == "bookworm" ] ; then 
    #  su -c 'echo "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";" > /etc/apt/apt.conf.d/no-bookworm-firmware.conf'
    #fi
		# # Create the file repository configuration:
	  # sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
		# # Import the repository signing key:
		# sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
		# # Update the package lists:
		# sudo DEBIAN_FRONTEND=noninteractive apt-get update
		# # Install the latest version of PostgreSQL.
		# # If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
		# if ! sudo DEBIAN_FRONTEND=noninteractive apt-get -y install postgresql ; then 
		# 	return 1
		# fi
		if ! sudo DEBIAN_FRONTEND=noninteractive apt-get -y install postgresql postgresql-client; then 
			return 1
		fi
   fi 
   return 0
}

_config_postgres() {
	#[ ! -r "${POSTGRES_RUN_PATH}" ] && [ ! -r "postgres.service" ] && return 
   #  cp postgres.service 
	# check /etc/system/multi-user.target.wants/postgresql.service 
	# started via /etc/rc2.d/S01postgresql

  [ -d "$POSTGRES_ETC_PATH" ] &&  sudo chown -R "$POSTGRES_RUN_USER":"$POSTGRES_RUN_GROUP" "$POSTGRES_ETC_PATH"
	[ -r "data.tar.gz" ] && sudo tar -C "$POSTGRES_LIB_PATH" -xf "data.tar.gz" && sudo chown -R "$POSTGRES_RUN_USER":"$POSTGRES_RUN_GROUP" "$POSTGRES_LIB_PATH"
   #[ "${POSTGRES_SYSTEMCTL_MODE}" == "enabled" ] && systemctl enable "$POSTGRES_PKG_NAME" --now
   #[ "${POSTGRES_SYSTEMCTL_MODE}" == "start" ] && systemctl start "$POSTGRES_PKG_NAME"

  _start_postgres
}

_remove_postgres() {
  sudo timeout -k 10 20 systemctl stop "$POSTGRES_PKG_NAME"
  sudo timeout -k 10 20 systemctl disable "$POSTGRES_PKG_NAME" 
  sudo rm -f "${POSTGRES_RUN_PATH}"
}

_start_postgres() {
  if [ "$POSTGRES_SYSTEMCTL_MODE" == "enabled" ] ; then 
    sudo timeout -k 10 20 systemctl enable "$POSTGRES_PKG_NAME" >/dev/null 2>&1
  else 
    sudo timeout -k 10 20 systemctl disable "$POSTGRES_PKG_NAME" >/dev/null 2>&1
  fi
  sudo timeout -k 10 20 systemctl restart "$POSTGRES_PKG_NAME" >/dev/null 2>&1
}
_restart_postgres() {
  sudo timeout -k 10 20 systemctl restart "$POSTGRES_PKG_NAME" >/dev/null 2>&1
}

if [ "$CMD_TSKSRVC" == "remove" ] ; then
   _remove_postgres
   exit
fi
if ! _init ; then
  echo "error postgres install"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_postgres && exit 0
if ! _config_postgres ; then 
  echo "error postgres config"
  exit 1
fi
if ! _start_postgres ; then 
  echo "error postgres start"
  exit 1
fi
exit 0
