#!/bin/bash

flag=$1
address=$2
hostname=$(hostname)
if [[ -z $address ]]; then
	echo "Please enter the local host address"
	exit 1
fi

if [[ -z $flag ]]; then
  flag=0
fi

ttn_url_bak=$address
ttn_url=$(echo $address | sed 's/\([.:]\)/\\\1/g')
systemctl stop ttnstack && sleep 5;


if [ $flag -le 1 ]; then
    echo "deb http://ports.ubuntu.com/ jammy main restricted universe multiverse" > /etc/apt/sources.list
    apt update > /dev/null 2>&1
    cp /etc/apt/sources.list.bak /etc/apt/sources.list
    apt install golang-cfssl > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "Installed golang-cfssl (Flag: $flag)"
        flag=2
    else
        echo "Error: Failed to install golang-cfssl (Flag: $flag)"
        exit 1
    fi
fi

if [ $flag -le 2 ]; then
    chown 886:886 /usr/lib/ttn-stack/acme
    if [ $? -eq 0 ]; then
        echo "Modified ownership of /usr/lib/ttn-stack/acme (Flag: $flag)"
        flag=3
    else
        echo "Error: Failed to modify ownership of /usr/lib/ttn-stack/acme (Flag: $flag)"
        exit 1
    fi
fi

if [ $flag -le 3 ]; then
    #ttn_url=$(cat /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml | grep console-url| sed "s/.*\b\([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}\)\b.*/\1/"|sed 's/\([.:]\)/\\\1/g')
	#ttn_url_bak=$(cat /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml | grep console-url| sed "s/.*\b\([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}\)\b.*/\1/")
	if [ -n "$ttn_url" ] && [ -n "$ttn_url_bak" ]; then
        echo "Modified $ttn_url and $ttn_url_bak (Flag: $flag)"
        flag=4
    else
        echo "Error: Failed to retrieve ttn_url and ttn_url_bak (Flag: $flag)"
        exit 1
    fi
fi

if [ $flag -le 4 ]; then
    cd /usr/lib/ttn-stack
    cat > /usr/lib/ttn-stack/ca.json <<EOF
{
  "names": [
    {"C": "NL", "ST": "Noord-Holland", "L": "Amsterdam", "O": "The Things Demo"}
  ]
}
EOF
    cfssl genkey -initca /usr/lib/ttn-stack/ca.json | cfssljson -bare ca
    if [ $? -eq 0 ]; then
        echo "Generated CA certificate (Flag: $flag)"
        flag=5
    else
        echo "Error: Failed to generate CA certificate (Flag: $flag)"
        exit 1
    fi
fi

if [ $flag -le 5 ]; then
    cat > /usr/lib/ttn-stack/cert.json <<EOF
{
  "hosts": ["thethings.example.com"],
  "names": [
    {"C": "NL", "ST": "Noord-Holland", "L": "Amsterdam", "O": "The Things Demo"}
  ]
}
EOF
    sed -i "s/thethings\.example\.com/$ttn_url/g" /usr/lib/ttn-stack/cert.json
    cfssl gencert -ca ca.pem -ca-key ca-key.pem /usr/lib/ttn-stack/cert.json | cfssljson -bare cert
	mv /usr/lib/ttn-stack/cert-key.pem /usr/lib/ttn-stack/key.pem
    if [ $? -eq 0 ]; then
		chown 886:886 /usr/lib/ttn-stack/cert.pem /usr/lib/ttn-stack/key.pem
        echo "Generated certificate (Flag: $flag)"
        flag=6
    else
        echo "Error: Failed to generate certificate (Flag: $flag)"
        exit 1
    fi
fi


if [ $flag -le 6 ]; then
	mv /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml.bak
	curl http://repo.dragino.com/release/tool/ttnstack/ttn-lw-stack-docker-open-source.yml -o /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml
	sed -i 's/thethings\.example\.com/'$ttn_url'/g' /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml
	if [ $? -eq 0 ]; then
        echo "Created OAuth client (Flag: $flag)"
        flag=7
    else
        echo "Error: Failed to create OAuth client (Flag: $flag)"
        exit 1
    fi
fi

if [ $flag -le 7 ]; then
    mv /usr/lib/ttn-stack/docker-compose.yml  /usr/lib/ttn-stack/docker-compose.yml.bak
    curl http://repo.dragino.com/release/tool/ttnstack/docker-compose-open-source.yml -o /usr/lib/ttn-stack/docker-compose.yml
	
	docker-compose run --rm stack is-db create-oauth-client \
      --id cli \
	  --name "Command Line Interface" \
	  --owner admin \
	  --no-secret \
	  --redirect-uri "local-callback" \
	  --redirect-uri "code"
	if [ $? -eq 0 ]; then
        echo "Created OAuth client (Flag: $flag)"
        flag=8
    else
        echo "Error: Failed to create OAuth client (Flag: $flag)"
        exit 1
    fi
    echo ${ttn_url_bak}
    docker-compose -f /usr/lib/ttn-stack/docker-compose.yml run --rm stack is-db create-oauth-client \
      --id console \
      --name "Console" \
      --owner admin \
      --secret "console" \
      --redirect-uri "https://${ttn_url_bak}/console/oauth/callback" \
      --redirect-uri "/console/oauth/callback" \
      --logout-redirect-uri "https://${ttn_url_bak}/console" \
      --logout-redirect-uri "/console"
    if [ $? -eq 0 ]; then
        echo "Created OAuth client (Flag: $flag)"
        flag=8
    else
        echo "Error: Failed to create OAuth client (Flag: $flag)"
        exit 1
    fi
fi

systemctl start ttnstack

echo "Script execution completed successfully"
