#!/usr/bin/env bash

echo ""
echo "If you've still got a problem after this"
if [ "$(uname)" = "Darwin" ] 
then
    echo "take a look here: https://nixos.org/manual/nix/stable/installation/installing-binary.html#macos"
else
    echo "take a look here: https://nixos.org/manual/nix/stable/installation/installing-binary.html#linux"
fi
echo "Okay?"
read

if [ -z "$NIX_ROOT" ]
then
    export NIX_ROOT="/nix"
fi



if [ "$(uname)" = "Darwin" ] 
then
    # TODO: make sure this fully removes LaunchDaemon: org.nixos.darwin-store
    sudo launchctl bootout system/org.nixos.darwin-store
    
    # check if file exists
    if [ -f "/Library/LaunchDaemon/org.nixos.nix-daemon.plist" ]
    then
        echo "removing LaunchDaemon1"
        sudo launchctl unload /Library/LaunchDaemon/org.nixos.nix-daemon.plist
        sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist
    fi
    
    
    # check if file exists
    if [ -f "/Library/LaunchDaemons/org.nixos.activate-system.plist" ]
    then
        echo "removing LaunchDaemon2"
        sudo launchctl unload /Library/LaunchDaemons/org.nixos.activate-system.plist
        sudo rm /Library/LaunchDaemons/org.nixos.activate-system.plist
    fi
    
    # if it was mounted
    if sudo diskutil list | grep 'Nix Store' &>/dev/null
    then
        echo "removing nix volume"
        # it was removed successfully
        if sudo diskutil apfs deleteVolume /nix
        then
            sudo diskutil apfs deleteVolume /nix && sudo rm -rf /nix/
        fi
        echo "may need to reboot for full effect"
    fi
    
    # check if file exists
    mount_filepath="/etc/synthetic.conf"
    if [ -f "$mount_filepath" ]
    then
        echo "removing $mount_filepath"
        # if file contains "nix"
        if cat "$mount_filepath" | grep 'nix' &>/dev/null
        then
            # remove nix from the file
            sudo mount_filepath="$mount_filepath" -- bash -c '
                sudo cat "$mount_filepath" | sed -E '"'"'s/nix\n?$//g'"'"' > "$mount_filepath"
            '
        fi
    fi
    
    # check if file exists
    if [ -f "/etc/fstab" ]; then
        write_to_fstab() {
            new_fstab_lines="$0"
            # vifs must be used to edit fstab
            # to make that work we  create a patch using "diff"
            # then tell vifs to use "patch" as an editor and apply the patch
            /usr/bin/diff /etc/fstab <(/usr/bin/printf "%s" "$new_fstab_lines") | EDITOR="/usr/bin/patch" sudo vifs
        }
        # if name_of_command doesnt exist
        if /usr/bin/grep "$NIX_ROOT apfs rw" /etc/fstab; then
            echo "Patching fstab"
            fstab_without_nix="$(/usr/bin/grep -v "$NIX_ROOT apfs rw" /etc/fstab)"
            write_to_fstab "$fstab_without_nix"
        fi
    fi
fi


delete_user () {
    user="$1"
    # logs them out by locking the account
    sudo passwd -l "$user" 2>/dev/null
    # kill all their processes
    sudo pkill -KILL -u "$user" 2>/dev/null
    # kill all their cron jobs
    sudo crontab -r -u "$user" 2>/dev/null
    # kill all their print jobs
    if [ -n "$(command -v "lprm")" ]
    then
        lprm -U "$user" 2>/dev/null
    fi
    # actually remove the user
    sudo deluser --remove-home "$user" 2>/dev/null # debian
    sudo userdel --remove "$user" 2>/dev/null # non-debian
}

remove_service () {
    service="$1"
    # if systemctl exists
    if [ -n "$(command -v "systemctl")" ]
    then
        sudo systemctl stop "$service"
        sudo systemctl disable "$service"
        sudo rm -f /etc/systemd/system/"$service"
        sudo rm -f /etc/systemd/system/"$service" # and symlinks that might be related
        sudo rm -f /usr/lib/systemd/system/"$service" 
        sudo rm -f /usr/lib/systemd/system/"$service" # and symlinks that might be related
        sudo systemctl daemon-reload
        sudo systemctl reset-failed
    fi
}

# stop the daemon
echo "removing service"
remove_service nix-daemon.service
remove_service nix-daemon.socket

# remove the users
echo "removing users"
delete_user nixbld1
delete_user nixbld2
delete_user nixbld3
delete_user nixbld4
delete_user nixbld5
delete_user nixbld6
delete_user nixbld7
delete_user nixbld8
delete_user nixbld9
delete_user nixbld10
delete_user nixbld11
delete_user nixbld12
delete_user nixbld13
delete_user nixbld14
delete_user nixbld15
delete_user nixbld16
delete_user nixbld17
delete_user nixbld18
delete_user nixbld19
delete_user nixbld20
delete_user nixbld21
delete_user nixbld22
delete_user nixbld23
delete_user nixbld24
delete_user nixbld25
delete_user nixbld26
delete_user nixbld27
delete_user nixbld28
delete_user nixbld29
delete_user nixbld30
delete_user nixbld31
delete_user nixbld32

# remove the group
echo "removing group"
sudo groupdel nixbld 2>/dev/null

# remove all the files
echo "removing all nixpkgs files"
sudo rm -rf /etc/nix /nix /var/root/.nix-profile /var/root/.nix-defexpr /var/root/.nix-channels "$HOME"/.nix-profile "$HOME"/.nix-defexpr "$HOME"/.nix-channels 2>/dev/null

# restore the shell files
echo "restoring any shell files"
if [ -f "/etc/bashrc.backup-before-nix" ]
then
    sudo mv /etc/bashrc.backup-before-nix /etc/bashrc
fi
if [ -f "/etc/profile.d/nix.sh.backup-before-nix" ]
then
    sudo mv /etc/profile.d/nix.sh.backup-before-nix /etc/profile.d/nix.sh
fi
if [ -f "/etc/zshrc.backup-before-nix" ]
then
    sudo mv /etc/zshrc.backup-before-nix /etc/zshrc
fi
if [ -f "/etc/bash.bashrc.backup-before-nix" ]
then
    sudo mv /etc/bash.bashrc.backup-before-nix /etc/bash.bashrc
fi

echo "final check"
if [ -e "$NIX_ROOT" ]
then
    sudo rm -rf "$NIX_ROOT" &>/dev/null
    sudo rm -f "$NIX_ROOT" &>/dev/null
    if [ -e "$NIX_ROOT" ]
    then
        echo 
        echo 
        echo 
        echo "Because your system mounted /nix during boot"
        echo "I believe you'll need to restart for changes to take affect"
    fi
fi