#!/bin/sh -ex

users() {
	UID_BASE="1800"
	UID_MAX="1900"
	GID="1800"

	USERS="
	user_gateway
	user_fetcher
	user_coremon
	user_stockpile
	user_monitoring_gateway
	user_alerting
	user_agent
	user_ingestor
	user_dumper
	user_memstore
	user_dataproxy
	user_yasmgateway
	user_name-resolver
	user_slicer
	user_backup
	user_project-manager
	user_mgstorage
	user_mgweb
	"
	GRP="group_solomon"
	HD="/nonexistent"

	groupadd -g $GID $GRP
	for USER in $USERS ; do
		useradd -K UID_MIN=$UID_BASE -K UID_MAX=$UID_MAX --gid $GID --home $HD --shell /bin/false $USER
	done
}

post_install() {
	# set timezone
	ln -vsf /usr/share/zoneinfo/Europe/Moscow /etc/localtime

	# set users
	users

	# set scripts
	chmod +x /usr/local/bin/fw.sh
	chmod +x /etc/dhcp/dhclient-enter-hooks.d/nonsupdate
	chmod +x /usr/local/bin/set_dc.py
	chmod +x /usr/local/bin/configs_setup.sh
	chmod +x /usr/local/bin/address_check.sh

	timedatectl set-ntp true
	systemctl enable systemd-timesyncd.service
	systemctl restart systemd-timesyncd.service

	for _app in fw-configure configs-setup dc-setup env-setup ; do
		if ! /bin/systemctl enable ${_app} ; then
			echo "Cannot enable ${_app} service!"
		fi
	done
}

case "$1" in
	configure)
		post_install
	;;
esac
