#!/bin/sh
# postinst script for tg-dashboard
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package

function init_tg_dashboard()
{
	echo -n "Fixing permissions..."
	chown -R sgadmin:sgadmin /etc/serverg
	echo "OK"

	# python-pam
	echo -n "Cheking python-pam..."
	pip3 show python-pam 1>/dev/null
	if [ $? == 0 ]; then
		echo "OK"
	else
		echo -n "Installing..."
		pip3 install python-pam
		echo "OK"
	fi

	# pyconcrete
	echo -n "Cheking pyconcrete..."
	pip3 show pyconcrete 1>/dev/null
	if [ $? == 0 ]; then
		echo "OK"
	else
		echo -n "Installing..."
		pip3 install pyconcrete 1>/dev/null <<EOF


EOF
		echo "OK"
	fi

	# google-cloud
	echo -n "Cheking google-cloud..."
	pip3 show google-cloud 1>/dev/null
	if [ $? == 0 ]; then
		echo "OK"
	else
		echo -n "Installing..."
		pip3 install --upgrade google-cloud 1>/dev/null
		echo "OK"
	fi

	# google-cloud-storage
	echo -n "Cheking google-cloud-storage..."
	pip3 show google-cloud-storage 1>/dev/null
	if [ $? == 0 ]; then
		echo "OK"
	else
		echo -n "Installing..."
		pip3 install --upgrade google-cloud-storage 1>/dev/null
		echo "OK"
	fi
	echo -n "Cheking gsutil..."
	pip3 show gsutil 1>/dev/null
	if [ $? == 0 ]; then
		echo "OK"
	else
		echo -n "Installing..."
		pip3 install gsutil 1>/dev/null
		echo "OK"
	fi
	# Start initializing script
	/opt/tg-dashboard/bin/tg-init.sh
}

case "$1" in
    configure)
	    init_tg_dashboard
    ;;

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

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
