###################################################################
# This file is for shell functions common between shells: zsh, bash
###################################################################

# Uses OSC52 to attempt to copy to client's clipboard through the terminal
function osc52() {
    CB_DATA="$@"
    echo -en "\033]52;c;$(echo $CB_DATA | base64)\a"
}

function osc52_file() {
    echo -en "\033]52;c;$(base64 $1 | tr -d '\n')\a"
}

function pubkey() {
    PUBKEY_PATH="$HOME/.ssh/id_rsa.pub"
    SUCCESS=0
    if [ -f "$PUBKEY_PATH" ]; then
        if which pbcopy &> /dev/null; then
            cat $PUBKEY_PATH | pbcopy
            SUCCESS=1
            echo "Copied public key [$PUBKEY_PATH] using pbcopy"
        fi

        if [ "$SUCCESS" = "0" ]; then
            osc52_file $PUBKEY_PATH
            echo "Copied public key [$PUBKEY_PATH] using osc52"
            SUCCESS=1
        fi

        if [ "$SUCCESS" = "0" ]; then
            echo "pbcopy not available - printing public key"
            echo
            cat $PUBKEY_PATH
        fi
    else
        echo "$PUBKEY_PATH does not exist"
    fi
}

function pwgen() {
    if [ -f ~/.dotfile_util/pwgen.py ]; then
        if which python &> /dev/null; then
            ~/.dotfile_util/pwgen.py 4
        else
            echo "Need python for pwgen"
        fi
    else
        echo "Could not find ~/.dotfile_util/pwgen.py"
    fi
}

function encrypt() {
    if which gpg &> /dev/null; then
        gpg --output $1.gpg --symmetric $1
        if [ $? -eq 0 ]; then
            echo "Encrypted file: $1.gpg"
        else
            echo "Something went wrong during encryption."
        fi
    else
        echo "Need gpg to encrypt"
    fi
}

function decrypt() {
    if which gpg &> /dev/null; then
        gpg --output $1.decrypted --decrypt $1
        if [ $? -eq 0 ]; then
            echo "Decrypted file: $1.decrypted"
        else
            echo "Something went wrong during decryption."
        fi
    else
        echo "Need gpg to decrypt"
    fi
}

function weather() {
    LOCATION=milwaukee

    curl wttr.in/$LOCATION
}

function fupdatedb() {
    if [ -f /tmp/flocate.db ]; then
        rm /tmp/flocate.db
    fi

    find / 2>/dev/null | gzip > /tmp/flocate.db
}

function flocate() {

    if [ ! -f /tmp/flocate.db ]; then
        echo "flocate.db does not exist: run fupdatedb"
        return -1
    fi

    zgrep $1 /tmp/flocate.db --color=auto
}

function install_pyenv() {
    if [ ! -d "$HOME/.pyenv" ]; then
        curl https://pyenv.run | bash
    else
        echo "pyenv is already installed"
        echo "remove ~/.pyenv to reinstall"
    fi

    echo "Installing python 3.9"
    pyenv install 3.9 -s

    echo "Creating sandbox virtualenv"
    pyenv virtualenv sandbox
}

function install_nvm() {
    if [ ! -f "$HOME/.nvm/nvm.sh" ]; then
        PROFILE=/dev/null curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
    else
        echo "nvm is already installed"
    fi
    nvm install --lts
}

function install_rust() {
    if [ ! -d "$HOME/.rustup" ]; then
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    else
        echo "rustup is already installed. remove ~/.rustup to reinstall"
    fi
}

function init_apt() {
    sudo apt-get install -y \
        libssl-dev \
        libncurses-dev \
        libbz2-dev \
        libffi-dev \
        libreadline-dev \
        libsqlite3-dev \
        liblzma-dev \
        python-is-python3 \
        python3-venv \
        build-essential \
        vim \
        tmux \
        net-tools \
        ripgrep \
        git \
}

function init_debian() {
    init_apt
    install_pyenv
    install_nvm
    install_rust
}

function autosetup() {
    if [ "$(uname)" = "Linux" ]; then
        IS_LINUX="1"
    else
        IS_LINUX="0"
    fi

    if [ "$IS_LINUX" = "1" ]; then
        which lsb_release > /dev/null 2>&1
        if [ "$?" = "0" ]; then
            DETECTED_DISTRO=$(lsb_release -a 2>/dev/null | grep Distributor | awk '{print $3}')
        else
            echo "Could not detect distribution"
        fi
    fi

    if [ "$IS_LINUX" = "1" ]; then
        echo "Setting up Linux: $DETECTED_DISTRO"

        case $DETECTED_DISTRO in
            Ubuntu*)
                init_debian
            ;;
            Debian*)
                init_debian
            ;;
            *)
                echo "Unknown distribution $DETECTED_DISTRO"
            ;;
        esac
    fi
    unset IS_LINUX DETECTED_DISTRO
}

function title {
    echo -ne "\033]0;"$*"\007"
}
