#!/bin/bash
# exit when any command fails
set -e

# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command stopped with exit code $?."' EXIT

eval "$(ucr shell)"

DATADIR="/etc/matterbridge"

mkdir -p $DATADIR

get_token () {
	local response="$1"
	token=$(cat "$response" | grep Token: | tr -d '[:space:]' | cut -d ':' -f2)
	echo $token
}

mattermost_login () {
	local server="$1"
	local username="$2"
	local password="$3"
	local data="{\"login_id\":\"$username\",\"password\":\"$password\"}"
	local url="$server/api/v4/users/login"
	local out=$(mktemp)
	curl -o $out -i -d "$data" "$url" || die 110 "Failed connect to Mattermost"
	local token="$(get_token $out)"
	rm $out
	if [ ! -z "${token// }" ]; then
		echo $token
		return 0
	fi
	return 1
}

# TODO automatically remove slashes from the end of the url?
CONFIG_HOSTNAME=$(ucr get mattermost/hostname)
if [ -z ${CONFIG_HOSTNAME} ]; then
	HOST="https://${hostname}.${domainname}:8234"
else
	HOST="https://$(ucr get mattermost/hostname)"
fi

if [ ! -e $DATADIR/mattermost-credentials ]; then
	echo "MMLogin=matterbridge" > $DATADIR/mattermost-credentials
	echo "MMPassword=$(openssl rand -base64 32)" >> $DATADIR/mattermost-credentials
	echo "MMTeamname=univention" >> $DATADIR/mattermost-credentials
	chmod 640 $DATADIR/mattermost-credentials
	source $DATADIR/mattermost-credentials
	univention-app shell mattermost mattermost-platform user create --email="$MMLogin@${domainname}" --password="$MMPassword" --username=$MMLogin
	univention-app shell mattermost mattermost-platform team add $MMTeamname $MMLogin
	univention-app shell mattermost mattermost-platform channel create --team $MMTeamname --name matterbridge1 --display_name "Matterbridge Demo Channel"
	univention-app shell mattermost mattermost-platform channel create --team $MMTeamname --name matterbridge2 --display_name "Matterbridge Demo Channel (mirror)"
fi
source $DATADIR/mattermost-credentials

# check if login is possible (Matterbridge may have been setup previously, but mattermost isn't running anymore)
mattermost_login $HOST $MMLogin $MMPassword

if [ ! -e $DATADIR/matterbridge.toml ]; then
cat <<-EOF >"$DATADIR/matterbridge.toml"
[mattermost]
[mattermost.univention]
#The mattermost hostname. (do not prefix it with http or https)
Server="${HOST#*//}"

#the team name as can be seen in the mattermost webinterface URL
#in lowercase, without spaces
Team="$MMTeamname"

#login/pass of your bot.
#Use a dedicated user for this and not your own!
Login="$MMLogin"
Password="$MMPassword"

RemoteNickFormat="<{NICK}> "
PrefixMessagesWithNick=true
# should be set to false when using a trusted ssl cert (the univention ca is not trusted inside the container)
SkipTLSVerify=true

[[gateway]]
name="gateway1"
enable=true
    [[gateway.inout]]
    account="mattermost.univention"
    channel="matterbridge1"

    [[gateway.inout]]
    account="mattermost.univention"
    channel="matterbridge2"

EOF

fi
