#!/usr/bin/env bash

GREEN="\e[32m"
LIGHTBLUE="\e[94m"
ENDCOLOR="\e[0m"

#

pushd /etc/nixos/

sudo wget https://git.sovransystems.com/Sovran_Systems/Sovran_Pro/raw/branch/main/For_NEW_Sovran_Pros_Upload/flake.nix

sudo chown root:root /etc/nixos/ -R

sudo chmod 770 /etc/nixos/ -R

popd

#

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/home/ /home

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/www/ /var/lib/www

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/domains/ /var/lib/domains

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/nextcloudaddition/ /var/lib/nextcloudaddition

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/njalla/ /var/lib/njalla

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/external_ip/ /var/lib/external_ip

sudo rsync -av -e "ssh -i /root/sovransystems" root@192.168.1.32:/var/lib/secrets/ /var/lib/secrets

#

nixos-rebuild switch --flake '/etc/nixos/#' --impure --update-input Sovran_Systems --commit-lock-file

#

sudo chown root:root /var/lib/secrets/main -R

sudo chown matrix-synapse:matrix-synapse /var/lib/secrets/matrix_reg_secret -R

sudo chown matrix-synapse:matrix-synapse /var/lib/secrets/matrixdb -R

sudo chown postgres:postgres /var/lib/secrets/nextclouddb -R

sudo chown turnserver:turnserver /var/lib/secrets/turn -R

sudo chown mysql:mysql /var/lib/secrets/wordpressdb -R

sudo chmod 770 /var/lib/secrets/ -R

#

sudo echo -e "${GREEN}What is your New Matrix (Element Chat) domain name?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/matrix

sudo echo -e "${GREEN}What is your New Wordpress domain name?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/wordpress

sudo echo -e "${GREEN}What is your New Nextcloud domain name?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/nextcloud

sudo echo -e "${GREEN}What is your New BTCPayserver domain name?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/btcpayserver

sudo echo -e "${GREEN}What is your New Vaultwarden domain name?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/vaultwarden

sudo echo -e "${GREEN}What is the email you would like to use to manage the SSL certificates for your domains?${ENDCOLOR}"
read 
sudo echo -n $REPLY > /var/lib/domains/sslemail


sudo chown caddy:php /var/lib/domains -R

sudo chmod 770 /var/lib/domains -R

#

set -x


sudo rm -rf /home/free/.config/BraveSoftware

sudo rm -rf /home/free/.local/share/fish/fish_history

#

sudo rm -rf /var/lib/www/wordpress/wp-config.php

sudo rm -rf /var/lib/www/nextcloud/config/config.php

sudo touch /var/lib/www/nextcloud/config/CAN_INSTALL

sudo sed -i '$e cat /var/lib/nextcloudaddition/nextcloudaddition' /var/lib/www/nextcloud/config/config.php


sudo chown caddy:php /var/lib/www -R

sudo chmod 770 /var/lib/www -R

#

sudo mkdir /var/lib/nextcloud

sudo chown caddy:php /var/lib/nextcloud -R

sudo chmod 770 /var/lib/nextcloud -R

#

sudo mkdir /var/lib/coturn

sudo chown turnserver:turnserver /var/lib/coturn -R

sudo chmod 770 /var/lib/coturn -R

#

sudo touch /var/lib/vaultwarden.env

sudo chown vaultwarden:vaultwarden /var/lib/vaultwarden.env

sudo chmod 770 /var/lib/vaultwarden.env

#

sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/nextclouddb 
sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/wordpressdb 
sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/matrixdb
sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/turn
sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/matrix_reg_secret
sudo echo -n $(pwgen -s 17 -1) > /var/lib/secrets/main
sudo echo -n ADMIN_TOKEN=$(openssl rand -base64 48
) > /var/lib/vaultwarden.env

#

sudo echo "root:$(cat /var/lib/secrets/main)" | chpasswd -c SHA512

#

flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
flatpak update

#

sudo echo "free:a" | chpasswd -c SHA512

#

sudo rm -rf /root/sp

sudo rm -rf /root/Step_2_psp

sudo rm -rf /root/.ssh

sudo rm -rf /home/free/.ssh

sudo rm -rf /root/sovransystems


sudo chown bitcoin:bitcoin /run/media/Second_Drive/BTCEcoandBackup/Bitcoin_Node -R

sudo chmod 770 /run/media/Second_Drive/BTCEcoandBackup/Bitcoin_Node -R

sudo chown electrs:electrs /run/media/Second_Drive/BTCEcoandBackup/Electrs_Data -R

sudo chmod 770 /run/media/Second_Drive/BTCEcoandBackup/Electrs_Data -R

nixos-rebuild switch --flake '/etc/nixos/#' --impure --update-input Sovran_Systems --commit-lock-file


#

set +x

sudo echo -e "${GREEN}These four passwords are generated for convenience to use for the Web front end setup UI accounts for Nextcloud, Wordpress, VaultWarden, and BTCPayserver (if you want to use them).${ENDCOLOR} \n"

sudo echo -e "$(pwgen -s 17 -1) \n"
sudo echo -e "$(pwgen -s 17 -1) \n"
sudo echo -e "$(pwgen -s 17 -1) \n"
sudo echo -e "$(pwgen -s 17 -1) \n"

#

sudo echo -e "${LIGHTBLUE}One last thing, you need to put the Njalla DDNS info from Njalla into njalla.sh.${ENDCOLOR} \n"

sudo echo -e "${GREEN}All Finished! Please Reboot then Enjoy your New Sovran Pro!${ENDCOLOR} \n"