#!/bin/bash

# Download necessary files
wget -nv https://up.tail.ws/my-packages.nix -O ~/my-packages.nix
wget -nv https://up.tail.ws/pacman-packages.txt -O ~/pacman-packages.txt
wget -nv https://raw.githubusercontent.com/emilwojcik93/aur-boost/main/aur-boost.sh -O "/tmp/aur-boost.sh"

# Set environment variables in ~/.profile
cat <<'EOF' > ~/.profile
# For Visual Studio Code
export NIXPKGS_ALLOW_UNFREE=1

# For .desktop files
export XDG_DATA_DIRS=$HOME/.nix-profile/share:$HOME/.share:"${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"

# For Rofi
export LC_ALL="C"
EOF

# Append Fcitx environment variables to /etc/environment
echo 'Adding Fcitx environment variables to /etc/environment'
echo 'GTK_IM_MODULE=fcitx' | sudo tee -a /etc/environment
echo 'QT_IM_MODULE=fcitx' | sudo tee -a /etc/environment
echo 'XMODIFIERS=@im=fcitx' | sudo tee -a /etc/environment

# Source ~/.profile in ~/.bash_profile
cat <<'EOF' > ~/.bash_profile
[[ -f ~/.profile ]] && . ~/.profile
EOF

# install Pacman packages
while IFS= read -r package; do
  sudo pacman -S --noconfirm "$package"
done < ~/pacman-packages.txt

# Install SDKMAN
echo 'Setting up SDKMAN'
curl -s "https://get.sdkman.io" | bash

# Install Zim framework
echo 'Setting up Zim'
wget -nv -O - https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh

sleep 6

# Install flatpaks
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak install flathub com.obsproject.Studio -y
flatpak install flathub io.github.cboxdoerfer.FSearch -y
flatpak install flathub moe.tundra.Tundra -y

# Configure ~/.zshrc
cat <<'EOF' >> ~/.zshrc
bindkey '`' autosuggest-accept

export XDG_DATA_DIRS="$XDG_DATA_DIRS:/var/lib/flatpak/exports/share:/home/$USER/.local/share/flatpak/exports/share"

# ssh - remote access
alias m1ssh="ssh blu@192.168.178.32";
alias ssh-snippetbox="ssh -p 764 -N -L 5000:127.0.0.1:5000 root@tail.ws &";
alias sshfsmounts="cd /sshfsmounts/20tb-1-raid1c3/";
alias tun-archm1-syncthing="ssh -L 8386:127.0.0.1:8384 -N blu@archm1 &; mullvad-browser http://localhost:8386 &";
alias tun-snippet-box="ssh -L 5000:127.0.0.1:5000 -N root@tail.ws -p 764 http://localhost:5000 &; sleep 1; mullvad-browser http://localhost:5000";
alias tun-syncthing="ssh -L 8384:127.0.0.1:8384 -N root@tail.ws -p 764 http://localhost:8384 &; sleep 1; mullvad-browser http://localhost:8384";

# text encryption
alias argon2v13="argon2-cli bluetail -v 13 -r | xclip -r; history -p; clear";

# networking
alias myip="curl -s icanhazip.com | tr -d '\n'";

# uploads
alias oxo="curl -F'file=@-' http://0x0.st'";
alias ixio="curl -F 'f:1=<-' ix.io";
EOF

# Improve build time using AUR-boost
chmod 755 "/tmp/aur-boost.sh" && sudo /tmp/aur-boost.sh

# Set up AUR and install yay
echo 'Setting up AUR'
mkdir ~/git
cd ~/git || exit
sudo pacman -S --needed git base-devel
git clone https://aur.archlinux.org/yay-bin.git
cd yay-bin || exit
makepkg -si  --noconfirm 

# Install python-pyfuse3 for borgbackup
yay -S python-pyfuse3 --noconfirm

# Create folders and set permissions
echo 'Making folders'
mkdir ~/tmpmount
sudo mkdir /sshfsmounts/20tb-1-raid1c3
sudo chown -R "$USER":"$USER" /sshfsmounts


echo awaiting ssh.zip at ~/Downloads
while true; do
    if [ -e ~/Downloads/ssh.zip ]; then
        echo "Found ssh.zip in ~/Downloads. Extracting to ~/.ssh"
        unzip ~/Downloads/ssh.zip -d ~/.ssh
        echo "Extraction complete."
        break
    fi
done

# Now relog
echo 'Now log out and log in again and then run sh fullscript2.sh to reload the profile and environment'
