#!/bin/bash

eval "$(/usr/sbin/ucr shell)"

set -e
set -x

. /usr/share/univention-lib/all.sh

check_site () {
	local url="$1"
	local tmp="$(mktemp)"
	curl "$url" || true
	curl -o /dev/null -s -w "%{http_code}" "$url" > $tmp
	test "404" == "$(< $tmp)" && return 1
	rm -f $tmp
	return 0
}

check_dns_entry () {
	local domain="$1"
	local timeout=60
	local timer=0
	while ! host -N 2 "$domain" >/dev/zero 2>&1 && [[ $timer -le timeout ]]; do
	    sleep 1
		(( timer+=1 ))
	done
	if ! host -N 2 "$domain" >/dev/zero 2>&1; then
		echo "ERROR: CNAME not created"
		exit 1
	fi
}

ucs_parseCredentials "$@"

test -n "$binddn" || false
test -n "$bindpwdfile" || false
uid="$(univention-ldapsearch -b "$binddn" -LLL uid | sed -n 's/^uid: //p')"

! univention-app info | grep dudle && univention-app install --noninteractive --username $uid --pwdfile $bindpwdfile dudle

# create new alias
udm dns/alias create "$@" --ignore_exists --superordinate="zoneName=$domainname,cn=dns,$ldap_base" --set name=dudle.local --set cname=$(hostname -f).
check_dns_entry "dudle.local"

# create certs
# TODO, use letsenrypt to create certificates
univention-install -y openssl
mkdir -p /root/test-ca/ && cd /root/test-ca
password=univention
openssl genrsa -des3 -out rootCA.key -passout pass:$password 2048
openssl req -batch -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt -passin pass:$password
openssl genrsa -out dudle.key 2048
openssl req -new -sha256 -key dudle.key -subj "/C=US/ST=CA/O=MyOrg, Inc./CN=dudle.local" -out dudle.csr
openssl x509 -req -in dudle.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out dudle.crt -days 500 -sha256 -passin pass:$password
cat dudle.crt rootCA.crt > chained_dudle.crt

# add to local ca store
cp rootCA.crt /usr/local/share/ca-certificates/
update-ca-certificates -f

# letsencrypt setup
DIR_LE="/etc/univention/letsencrypt"
cp chained_dudle.crt "$DIR_LE/signed_chain.crt"
cp dudle.key "$DIR_LE/domain.key"
chmod 0644 "$DIR_LE/signed_chain.crt"
chmod 0644 "$DIR_LE/domain.key"
chown letsencrypt:root "$DIR_LE/signed_chain.crt"
chown letsencrypt:root "$DIR_LE/domain.key"
ucr set letsencrypt/domains=dudle.local
ucr set letsencrypt/services/apache2=true
run-parts --verbose "$DIR_LE/setup.d/"
run-parts --verbose "$DIR_LE/post-refresh.d/"

# tests
check_site https://dudle.local/dudle/
check_site https://$(hostname -f)/dudle/
check_site https://$(hostname -f)/
check_site https://dudle.local

! univention-app info | grep nextcloud && univention-app install --noninteractive --username $uid --pwdfile $bindpwdfile nextcloud
check_site https://dudle.local/nextcloud/
check_site https://$(hostname -f)/nextcloud/

#! univention-app info | grep admin-dashboard && univention-app install --noninteractive --username $uid --pwdfile #$bindpwdfile admin-dashboard
#check_site https://dudle.local/ucs-dashboard/
#check_site https://$(hostname -f)/ucs-dashboard/
#check_site https://dudle.local/metrics-prometheus/
#check_site https://$(hostname -f)/metrics-prometheus/
#check_site https://dudle.local/metrics-node/
#check_site https://$(hostname -f)/metrics-node/