#!/bin/bash
list_versions() {
    echo "installed versions:"
    for i in "$HOME"/.zig/zig0*; do basename "$i"; done
    echo ""
}

install_version() {
    check_valid_patch $1
    if [[ $valid != 'true' ]]; then
        echo "invalid version $1"
        echo "using $(zig version)"
        exit 1
    fi

    fname="$HOME/.zig/downloads/zig-$zigos-$zigarch-$1"
    tarball_name="$fname.tar.xz"

    url_subdir="download/$1"
    if [[ $1 =~ .*dev.* ]]; then
        url_subdir="builds"
    fi

    if [[ ! -f $fname ]]; then
        curl -Lo $tarball_name https://ziglang.org/$url_subdir/zig-$zigos-$zigarch-$1.tar.xz
    fi
    cd $download_dir
    tar -xJf $tarball_name
    mv $fname/ ../$1
    check_exists $1
    link_version $1

    install_modules
}

link_version() {
    if [[ $exists == 'true' ]]; then
        ln -sfn $zig_dir/$1 $zig_dir/zig
    fi
}

check_valid_minor() {
    if [[ $1 =~ 0\.[0-9]{1,2} ]]; then
        valid='true'
    fi
}

check_valid_patch() {
    if [[ $1 =~ 0\.[0-9]{1,2}\.[0-9]{1,2}.* ]]; then
        valid='true'
    fi
}

check_exists() {
    if [[ -d $zig_dir/$1 ]]; then
        exists='true'
    fi
}

remove_version() {
    check_exists $1
    if [[ $exists == 'true' ]]; then
        if [[ $current_link == $1 ]]; then
            echo "cannot remove version $1 because it is currently in use"
        else
            rm -rf $zig_dir/$1
            echo "removed version $1"
        fi
    else
        echo "version $1 not installed"
    fi
}

get_latest_patch_for_minor() {
    version=$(curl -s "https://ziglang.org/download/index.json" | grep -o "zig-linux-x86_64-$1.*.tar.xz" | head -n 1 | tr -d '\r\n' | sed 's/zig-linux-x86_64-\(.*\).tar.xz/\1/g')
    if [[ $version == '' ]]; then
        echo "could not determine latest version for $1."
        exit 1
    else
        echo "latest $1 patch: $version"
    fi
}

get_latest_patch_for_nightly() {
    get_latest_patch_for_minor
}

prompt_install() {
    if [[ "$1" == "" ]]; then
        echo "installing nightly build"
        valid=true
        get_latest_patch_for_nightly
    else 
        echo "$1 not found in $zig_dir"
        list_versions
        check_valid_patch $1
        version=$1
    fi

    if [[ $valid == 'true' ]]; then
        echo "attempt to download and install? [y/n]"
        read input
        if [[ $input == 'y' ]]; then
            install_version $version
        fi
    else
        check_valid_minor $version
        if [[ $valid == 'true' ]]; then
            get_latest_patch_for_minor $version
            check_exists $version
            if [[ $exists == 'true' ]]; then
               link_version $version
            else
                prompt_install $version
            fi
        else
            echo "invalid version $version"
            echo "using $(zig version)"
            exit 1
        fi
    fi
}

install_modules() {
    echo "no modules to install for zig"
}
#     fname="$HOME/.config/mygvm/modules_to_install"
#     echo "attempting to install nvim required binaries and modules at $fname"
#     [[ ! -f $fname ]] && echo "no modules file at $fname" && return

#     while IFS= read -r line; do
#     if [[ "$line" != "" ]]; then 
#         echo "running go install $line"
#         go install $line
#     fi
#     done < "$HOME/.config/mygvm/modules_to_install"

#     [[ $(which nvim) ]] && nvim --headless +GoInstallBinaries +q
#     echo ""
# }

zig_dir="$HOME/.zig"
download_dir="$zig_dir/downloads"
mkdir -p "$download_dir"
current_link=$(basename $(readlink $HOME/.zig/zig))
uname_m=$(uname -m)
zigarch='x86_64'
if [[ $uname_m == 'arm64' || $uname_m == 'aarch64' ]]; then
    zigarch='aarch64'
fi
zigos='linux'
if [[ $(uname) == 'Darwin' ]]; then
    zigos='darwin'
fi
valid='false'
exists='false'

version=''

check_exists $1
if [[ $1 == '' ]]; then
    list_versions
elif [[ $1 == 'list' ]]; then
    list_versions
elif [[ $1 == 'remove' ]]; then
    remove_version $2
elif [[ $1 == 'uninstall' ]]; then
    remove_version $2
elif [[ $1 == 'install' ]]; then
    install_version $2
elif [[ $1 == 'use' ]]; then
    if [[ $2 == 'nightly' ]]; then
        version_to_use=
        exists=false
    else
        version_to_use=$2
        check_exists $version_to_use
    fi
    if [[ $exists == true ]]; then
        link_version $version_to_use
    else
        prompt_install $version_to_use
    fi
    install_modules
elif [[ $1 == 'modules' ]]; then
    install_modules
elif [[ $exists == 'true' ]]; then
    link_version $1
else
    prompt_install $1
fi

echo "using $(zig version)"

