#!/bin/sh -e


jf() {
	local _src_file=$1 _dst_file=$2 _user=$3 _fld=$4 _tmp_file

	_tmp_file="${_dst_file}.tmp"
	touch $_tmp_file
	chmod 400 $_tmp_file
	chown $_user $_tmp_file
	jq -r .$_fld $_src_file > $_tmp_file
	mv -vf $_tmp_file $_dst_file
}


post_install() {
	APP="agent"
	GROUP="group_solomon"
	CLOUD_APP="solomon-agent"
	PORTOCTL="/usr/sbin/portoctl"
	CONF_DIR="/etc/yandex-solomon-agent"
	SEC_DIR="/Berkanavt/solomon/secrets"
	LOGS_DIR="/logs"

	ENV="$(cat /etc/solomon/env 2>/dev/null)"

	if [ ! -e ${LOGS_DIR} ]; then
		/usr/bin/install -g ${GROUP} -m 775 -d ${LOGS_DIR}
	fi

	if [ "$ENV" = "cloud-preprod" -o "$ENV" = "cloud-prod" ] && hostname | grep -qE "^solomon-gateway" ; then
		USER="user_agent"
		SECRET_FILE="$SEC_DIR/gateway_agent.secret"
		PUBLIC_KEY_FILE="$SEC_DIR/gateway_agent_public.key"
		PRIVATE_KEY_FILE="$SEC_DIR/gateway_agent_private.key"

		if [ ! -d "$SEC_DIR" ] ; then
			mkdir $SEC_DIR
		fi
		/usr/local/bin/secrets decrypt \
			--in /Berkanavt/solomon/configs/gateway_agent.${ENV}.secret \
			--out $SECRET_FILE \
			--user $USER

		ACCOUNT_ID="$(jq -r .service_account_id $SECRET_FILE)"
		KEY_ID="$(jq -r .id $SECRET_FILE)"
		if [ "$ENV" = "cloud-preprod" ] ; then
			CLUSTER="preprod"
			CLOUD_TYPE="CLOUD_PREPROD"
		else
			CLUSTER="production"
			CLOUD_TYPE="CLOUD_PROD"
		fi
		jf $SECRET_FILE $PUBLIC_KEY_FILE $USER public_key
		jf $SECRET_FILE $PRIVATE_KEY_FILE $USER private_key

		cp -vf $CONF_DIR/agent_cloud_gateway.conf $CONF_DIR/agent.conf.tmp
		sed -i "s/CLUSTER/${CLUSTER}/;
			s/CLOUD_TYPE/${CLOUD_TYPE}/;
			s/ACCOUNT_ID/${ACCOUNT_ID}/;
			s/KEY_ID/${KEY_ID}/" $CONF_DIR/agent.conf.tmp
		mv -vf $CONF_DIR/agent.conf.tmp $CONF_DIR/agent.conf
	else
		ln -vsf $CONF_DIR/agent_generic.conf $CONF_DIR/agent.conf
	fi

	# If in container
	if [ -x $PORTOCTL ] && [ "$($PORTOCTL get self absolute_name)" != "/" ] ; then 
		ln -vsf $CONF_DIR/porto.conf $CONF_DIR/services/porto.conf
	fi

	# Disable the Agent instance brought by Cloud's SALT
	if [ -x /bin/systemctl -a -f /etc/systemd/system/${CLOUD_APP}.service ] ; then
		if ! /bin/systemctl stop ${CLOUD_APP} ; then
			echo "Cannot stop ${CLOUD_APP} service!"
		fi

		if ! /bin/systemctl disable ${CLOUD_APP} ; then
			echo "Cannot disable ${CLOUD_APP} service!"
		fi
	elif [ -x /usr/bin/service -a -f /etc/init/${CLOUD_APP}.conf ] ; then
		if ! /usr/bin/service ${CLOUD_APP} stop ; then
			echo "Cannot stop ${CLOUD_APP} service!"
		fi

		if ! echo "manual" >> /etc/init/${CLOUD_APP}.override ; then
			echo "Cannot disable ${CLOUD_APP} service!"
		fi
	fi

	# And start our Agent
	if [ -x /bin/systemctl -a -f /etc/systemd/system/yandex-solomon-${APP}.service ] ; then
		if ! /bin/systemctl enable yandex-solomon-${APP} ; then
			echo "Cannot enable ${APP} service!"
		fi
		if ! /bin/systemctl restart yandex-solomon-${APP} ; then
			echo "Cannot restart ${APP} service!"
		fi
	elif [ -x /usr/bin/service -a -f /etc/init/yandex-solomon-${APP}.conf ] ; then
		if ! /usr/bin/service yandex-solomon-${APP} restart ; then
			echo "Cannot restart ${APP} service!"
		fi
	fi
}


#-------------------------------------------------------------------------


case "$1" in
	configure)
		post_install
	;;

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

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

#DEBHELPER#

exit 0
