#!/usr/bin/env zsh

echo "Updating software..."
sudo apt update
sudo apt full-upgrade -y

echo "Installing software..."
#install necessary tools
sudo apt -q -y install autojump\
    curl\
    git\
    build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev curl git \
    libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
    bat\
    zsh\
    tmux\
    emacs-nox\
    gdb\
    python3-pip\
    kali-defaults\
    kali-root-login\
    xrdp\
    kali-tools-802-11\
    kali-tools-exploitation\
    kali-tools-fuzzing\
    kali-tools-identify\
    kali-tools-information-gathering\
    kali-tools-post-exploitation\
    kali-tools-reverse-engineering\
    kali-tools-sniffing-spoofing\
    kali-tools-vulnerability\
    kali-tools-wireless\
    eza\
    groff\
    bc\
    lshw\
    shfmt\
    xml-twig-tools\
    cmake\
    lolcat\
    cowsay\
    figlet\
    dos2unix\
    fastfetch\
    avahi-daemon\
    keychain\
    multitail\
    ghidra\
    zlib1g-dev\
    liblzma-dev\
    liblzo2-dev\
    docker.io\
    ruby-dev\
    gdb-multiarch\
    fzf\
    gdbserver\
    gawk\
    virtualenv\
    sipcalc\
    kismet\
    direnv\
    tmuxinator\
    python3-scapy\
    python3-netifaces\
    wireshark\
    kali-grant-root\
    lua5.4\
    mosh\
    hcxtools

if [ $? -ne 0 ]; then
    echo "apt installation failure"
    echo "Please correct the error and run this script again"
    exit
fi

# Install Zeek via OBS binary packages (Kali is Debian Testing-based).
# The 'zeek' package always tracks the latest release.
if ! command -v zeek > /dev/null 2>&1; then
    echo 'deb https://download.opensuse.org/repositories/security:/zeek/Debian_Testing/ /' \
        | sudo tee /etc/apt/sources.list.d/security:zeek.list
    curl -fsSL https://download.opensuse.org/repositories/security:zeek/Debian_Testing/Release.key \
        | gpg --dearmor \
        | sudo tee /etc/apt/trusted.gpg.d/security_zeek.gpg > /dev/null
    sudo apt update
    sudo apt install -y zeek
    if [ $? -ne 0 ]; then
        echo "Zeek installation failure"
        echo "Please correct the error and run this script again"
        exit
    fi
fi

type colorls > /dev/null 2>&1
if [ $? -ne 0 ]; then
    sudo gem install colorls mdless
    if [ $? -ne 0 ]; then
        echo "gem installation failure"
        echo "Please correct the error and run this script again"
        exit
    fi
fi

mkdir -p $HOME/bin
mkdir -p $HOME/clones

#enable i386 and amd64 binary support (ONLY RUN ON ARM HOSTS!)
#uncomment if you desire this functionality
# sudo apt install -y qemu-user-static binfmt-support
# sudo dpkg --add-architecture amd64
# sudo dpkg --add-architecture i386
# sudo apt update
# sudo apt install libc6:amd64
# sudo apt install libc6:i386

# you can also enable arm binary support on amd64 hosts if you want to, for whatever reason
# sudo apt install -y qemu-user-static binfmt-support
# sudo dpkg --add-architecture aarch64
# sudo dpkg --add-architecture armhf
# sudo apt update
# sudo apt install libc6:aarch64
# sudo apt install libc6:armhf

#install a known version of python and use that as the default

type pyenv > /dev/null 2>&1
if [ $? -ne 0 ]; then
    curl https://pyenv.run | bash

    if [ $? -ne 0 ]; then
        echo "pyenv installation failure"
        echo "Please correct the error and run this script again"
        exit
    fi

    PYENV_ROOT="$HOME/.pyenv"
    PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"

    pyenv install 3.13.7
    pyenv global 3.13.7
fi

PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

pip install --upgrade pip requests python-dateutil scapy dpkt boofuzz

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$HOME/.cargo/bin/cargo install mcat
$HOME/.cargo/bin/cargo install atuin


curl -qsL 'https://install.pwndbg.re' | sh -s -- -t pwndbg-gdb

# Install antidote (zsh plugin manager used by .zshrc config)
if [ ! -d $HOME/.antidote ]; then
    git clone --depth=1 https://github.com/mattmc3/antidote.git $HOME/.antidote
fi

# Kali/Debian installs bat as batcat; create a bat symlink for scripts that expect it
if ! command -v bat > /dev/null 2>&1 && command -v batcat > /dev/null 2>&1; then
    sudo ln -sf $(which batcat) /usr/local/bin/bat
fi

if [ ! -d $HOME/clones/sunwait ]; then
    git clone https://github.com/risacher/sunwait.git $HOME/clones/sunwait
    cd $HOME/clones/sunwait
    make
    ln -s $HOME/clones/sunwait/sunwait $HOME/bin/sunwait
    cd $HOME
fi

if [ ! -d $HOME/clones/bat-extras ]; then
    git clone https://github.com/eth-p/bat-extras.git $HOME/clones/bat-extras
    cd $HOME/clones/bat-extras
    sudo ./build.sh --install
    cd $HOME
fi

if [ ! -d $HOME/clones/diff-so-fancy ]; then
    git clone https://github.com/so-fancy/diff-so-fancy.git $HOME/clones/diff-so-fancy
fi

if [ ! -d $HOME/.tmux/plugins/tpm ]; then
    mkdir -p $HOME/.tmux/plugins/tpm
    git clone https://github.com/tmux-plugins/tpm $HOME/.tmux/plugins/tpm
fi

# tmux-powerline is referenced in statusbar.conf; must be present before tmux starts
if [ ! -d $HOME/clones/tmux-powerline ]; then
    git clone https://github.com/erikw/tmux-powerline.git $HOME/clones/tmux-powerline
fi

# tmux-mem-cpu-load is a C binary used by statusbar.conf; cmake is installed above
if [ ! -d $HOME/clones/tmux-mem-cpu-load ]; then
    git clone https://github.com/thewtex/tmux-mem-cpu-load.git $HOME/clones/tmux-mem-cpu-load
    cmake -S $HOME/clones/tmux-mem-cpu-load -B $HOME/clones/tmux-mem-cpu-load/build
    cmake --build $HOME/clones/tmux-mem-cpu-load/build
    ln -sf $HOME/clones/tmux-mem-cpu-load/build/tmux-mem-cpu-load $HOME/bin/tmux-mem-cpu-load
fi

if [ ! -d $HOME/clones/nerd-fonts ]; then
    git clone --filter=blob:none --sparse https://github.com/ryanoasis/nerd-fonts.git $HOME/clones/nerd-fonts
    cd $HOME/clones/nerd-fonts
    git sparse-checkout add patched-fonts/Hack
    ./install.sh
    cd $HOME
fi

if [ ! -d $HOME/clones/splitmind ]; then
    git clone https://github.com/jerdna-regeiz/splitmind.git $HOME/clones/splitmind
fi

if [ ! -d $HOME/clones/astral ]; then
    git clone https://github.com/sffjunkie/astral.git $HOME/clones/astral
fi

if [ ! -e $HOME/bin/diff-so-fancy ]; then
    ln -s $HOME/clones/diff-so-fancy/diff-so-fancy $HOME/bin/diff-so-fancy
fi

sudo chsh -s $(which zsh) $(whoami)
sudo usermod -aG docker,kali-trusted,wireshark $(whoami)

# Download shell config archive and deploy; skip if already present
if [ ! -d $HOME/shell ]; then
    curl https://web.cecs.pdx.edu/~dmcgrath/setup.tar.bz2 | tar xjvf - -C ~/
fi
$HOME/shell/deploy.zsh

sudo systemctl enable --now avahi-daemon
sudo systemctl enable --now docker
sudo systemctl enable --now xrdp
sudo systemctl enable --now ssh


###############=====================################
###############= git configuration =################
git config unset --global user.name
#fill in and uncomment the next two lines!
#git config --global user.name ""
#git config --global user.email ""
git config get --global user.name > /dev/null
if [ $? -ne 0 ]; then
    echo "Please set your git user.name and user.email!"
    echo "You were prompted to do this, but you didn't!"
    echo "git config --global user.name \"Your Name Here\""
    echo "git config --global user.email \"ODIN@pdx.edu\""
fi


git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"
git config --global interactive.diffFilter "diff-so-fancy --patch"
git config --global color.ui true
git config --global color.diff-highlight.oldNormal    "red bold"
git config --global color.diff-highlight.oldHighlight "red bold 52"
git config --global color.diff-highlight.newNormal    "green bold"
git config --global color.diff-highlight.newHighlight "green bold 22"
git config --global color.diff.meta       "11"
git config --global color.diff.frag       "magenta bold"
git config --global color.diff.func       "146 bold"
git config --global color.diff.commit     "yellow bold"
git config --global color.diff.old        "red bold"
git config --global color.diff.new        "green bold"
git config --global color.diff.whitespace "red reverse"

exec zsh
