#! /bin/sh

set -e

if [ -f /etc/gitlab-common/gitlab-common.conf ]; then
  . /etc/gitlab-common/gitlab-common.conf
else
  echo "Failed to find /etc/gitlab-common/gitlab-common.conf."
  exit 1
fi

case "$1" in
  configure)
    # Source varibales
    . /etc/gitaly/gitlab-common.conf
    # # Show debconf questions
    # . /usr/share/debconf/confmodule
    # . /usr/share/dbconfig-common/dpkg/postinst
	. /usr/lib/gitaly/scripts/gitaly-download.sh

    # Create diaspora user
    ${gitaly_scripts}/adduser.sh
    # Create assets cache directory
    mkdir -p ${gitaly_cache}
    chown ${gitaly_user}: ${gitaly_cache}
    service_path=/etc/systemd/system/gitaly.service.d
     mkdir -p ${service_path}
     if [ -e ${service_path}/override.conf ]; then
       echo "${service_path}/override.conf already exist"
       # Make sure only gitlab user is updated
       sed -i "s/^ *User=.*/User=${gitlab_user}/" ${service_path}/override.conf
     else
       printf "[Service]\nUser=${gitlab_user}\n" > ${service_path}/override.conf
     fi
     # Check if storage path exist and create if required
     export $(grep '^\s*path\s*=' /etc/gitaly/config.toml | sed 's/ //g' | sed 's/"//g' | sed "s/'//g" )
     if ! [ -d $path ] ; then runuser -u ${gitlab_user} -- sh -c 'mkdir $path'; fi
   ;;

   triggered)
    
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac


#DEBHELPER#

exit 0
