echo "In order for this script to run properly, please make sure to port forward port 443 on this computer via your router's admin page or phone app and that a domain name points to this computer's public ip address"
read -p "Please enter this computer's domain name: " domain

pacman -Syy archlinux-keyring
pacman -S matrix-synapse caddy

cd /var/lib/synapse

sudo -u synapse python -m synapse.app.homeserver --server-name $domain --config-path /etc/synapse/homeserver.yaml --generate-config --report-stats=no

echo "$domain {" > /etc/caddy/Caddyfile
echo "reverse_proxy /_matrix/* localhost:8008" >> /etc/caddy/Caddyfile
echo "reverse_proxy /_synapse/client/* localhost:8008" >> /etc/caddy/Caddyfile
echo "}" >> /etc/caddy/Caddyfile

echo "max_upload_size: 1000M" >> /etc/synapse/homeserver.yaml

systemctl enable --now synapse caddy

echo 'register_new_matrix_user -u $1 -c /etc/synapse/homeserver.yaml' > /usr/local/bin/mka
chmod +x /usr/local/bin/mka
echo "run 'vim /etc/synapse/homeserver.yaml' to change the max upload size, in megabytes, currently 1000M"
echo "Use the command 'mka' then a desired one word username to add users, e.g. 'mka replacewithusername'"
