#!/usr/bin/env bash

echo "Rustup is available in debian and will install the latest rustc and cargo for a user account."

exit 0

cd /var/tmp

USER=rust-lang
PROJECT=rustup
PRERELEASE=false
# RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
RELEASE=$(lastversion --format version ${USER}/${PROJECT})
# VERSION=$(echo "${RELEASE}" | cut -c 2- | cut -f1 -d"-")
# VERSION=$(date +%Y-%m-%d_)git
VERSION=${RELEASE}
DOWNLOAD_DIR=/tmp

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
#xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# make a containing directory
mkdir -p /var/tmp/${PROJECT}-"${RELEASE}"
cd /var/tmp/${PROJECT}-"${RELEASE}" || exit

cat >./description-pak <<EOF
The Rustup tool installs and manages the Rust development environment. 
EOF

cat >./checkinstall_it.sh <<'EOF'

echo "ENTERING CHECKINSTALL"

BASE_URL='https://api.github.com/repos'
BASE_USER='rust-lang'
BASE_REPO='rustup'
LICENSE_PAGE_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/license
RELEASES_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/releases

# CONTENT=$(curl -s ${RELEASES_URL}/latest)
# LATEST_URL=$(echo "${CONTENT}" | jq --raw-output '.assets[] | select(.browser_download_url | test("Linux-x86_64")) | .browser_download_url')

echo "LATEST_URL: " "${LATEST_URL}"

# RELEASE=$(echo "${CONTENT}" | jq --raw-output '.tag_name')

# LICENSE_URL=$(curl "${LICENSE_PAGE_URL}" | jq --raw-output '.download_url')
# wget -c ${LICENSE_URL}

VERSION=$(lastversion --format version ${BASE_USER}/${BASE_REPO})
RELEASE="1"
REQUIRES=""

# make a new temporary directory for this use
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
mkdir -p ${BASE_TMP_DIR}

# do your work
checkinstall -y --fstrans \
	--pkgname=rustup \
	--pkgversion=${VERSION}\
	--pkgrelease=${RELEASE} \
	--pkgarch=amd64 \
	--pkggroup=admin \
	--pkglicense=LICENSE \
 	--pkgsource=${LATEST_URL} \
 	--maintainer=cyteen@ring-zero.co.uk \
 	--requires=${REQUIRES} \
 	-D \
 	bash ./install.sh
EOF

cat >./install.sh <<'EOF'
# https://hoverbear.org/blog/setting-up-a-rust-devenv/

export CARGO_HOME=${HOME}/.cargo/bin
export RUSTUP_HOME=${HOME}/.rustup



#    RUSTUP_HOME (default: ~/.rustup or %USERPROFILE%/.rustup) Sets the root rustup folder, used for storing installed toolchains and configuration options.
# RUSTUP_HOME	

#    RUSTUP_TOOLCHAIN (default: none) If set, will override the toolchain used for all rust tool invocations. A toolchain with this name should be installed, or invocations will fail.
# RUSTUP_TOOLCHAIN

#    RUSTUP_DIST_SERVER (default: https://static.rust-lang.org) Sets the root URL for downloading static resources related to Rust. You can change this to instead use a local mirror, or to test the binaries from the staging directory.
# RUSTUP_DIST_SERVER

#    RUSTUP_DIST_ROOT (default: https://static.rust-lang.org/dist) Deprecated. Use RUSTUP_DIST_SERVER instead.
# RUSTUP_DIST_ROOT

#    RUSTUP_UPDATE_ROOT (default https://static.rust-lang.org/rustup) Sets the root URL for downloading self-updates.
# RUSTUP_UPDATE_ROOT

#    RUSTUP_IO_THREADS unstable (defaults to reported cpu count). Sets the number of threads to perform close IO in. Set to disabled to force single-threaded IO for troubleshooting, or an arbitrary number to override automatic detection.
# RUSTUP_IO_THREADS 

#    RUSTUP_TRACE_DIR unstable (default: no tracing) Enables tracing and determines the directory that traces will be written too. Traces are of the form PID.trace. Traces can be read by the Catapult project tracing viewer.
# RUSTUP_TRACE_DIR

#    RUSTUP_UNPACK_RAM unstable (default 400M, min 100M) Caps the amount of RAM rustup will use for IO tasks while unpacking.
# RUSTUP_UNPACK_RAM 

#    RUSTUP_NO_BACKTRACE Disables backtraces on non-panic errors even when RUST_BACKTRACE is set.
# RUSTUP_NO_BACKTRACE


curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

## Completions
# Bash
rustup completions bash > ~/.local/share/bash-completion/completions/rustup

# Zsh completions
rustup completions zsh > ~/.zfunc/_rustup


# update the current shell with the changes
source $HOME/.cargo/env

# install the Rust source and documentation locally
rustup component add rust-src
rustup component add rust-docs

# rust-clippy linter
cargo +nightly install clippy

# rustfmt standard automated styling
cargo install rustfmt

# racer code completion utility
cargo install racer

# set path to rust sources in the environment
export RUST_SRC_PATH=${HOME}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src

# rls is a language server implementation for Rust. It provides things like code completion, goto definitions, rich refactoring
rustup component add rls --toolchain nightly-x86_64-unknown-linux-gnu
rustup component add rust-analysis --toolchain nightly-x86_64-unknown-linux-gnu
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu

export DYLD_LIBRARY_PATH=${HOME}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib
export RLS_ROOT=${HOME}/git/rust/rls
EOF

bash ./checkinstall_it.sh
