#!/bin/sh

DATADIR='/var/lib/uptime-kuma'

if [ "${0##*.}" = 'post-upgrade' ]; then
	cat >&2 <<-EOF
	*
	* If upgrading from v1, please give time for database migration. This can take a while.
	* You can follow the current state of migration via tail -f /var/log/uptime-kuma/uptime-kuma.log.
	*
	* If a migration to mariadb is desirable, follow https://forge.ilot.io/ilot/iports/wiki/Upgrade+Uptime-Kuma+to+v2.-
	*
	EOF
	
	exit 0
else
	cat >&2 <<-EOF
	*
	* If statisfied with a file-based sqlite3 database, simply starting the uptime-kuma service
	* is enough to start setting up service by accessing localhost:3001
	*
	* It is possible to use mariadb for better performance by setting up database and adjusting config
	* in /var/lib/uptime-kuma/db-config.json using the following syntax:
	* 
	* {
	*     "type": "mariadb",
	*     "port": 3306,
	*     "host": "localhost",
	*     "username": "uptime_kuma",
	*     "password": "db-password",
	*     "dbName": "uptime_kuma"
	* }
	*
	EOF

	if ! getent group uptime-kuma 1>/dev/null; then
		echo '* Creating group uptime-kuma' 1>&2

		addgroup -S uptime-kuma
	fi

	if ! id uptime-kuma 2>/dev/null 1>&2; then
		echo '* Creating user uptime-kuma' 1>&2

		adduser -DHS -G uptime-kuma -h "$DATADIR" -s /bin/sh  \
			-g "added by apk for uptime-kuma" uptime-kuma
		passwd -u uptime-kuma 1>/dev/null  # unlock
	fi

	if ! id -Gn uptime-kuma | grep -Fq www-data; then
		echo '* Adding user uptime-kuma to group www-data' 1>&2

		addgroup uptime-kuma www-data
	fi

	exit 0
fi


