#!/usr/bin/env bash

apt install -y golang-go

cd /var/tmp || exit

USER="k3d-io"
PROJECT="k3d"
PRERELEASE="false"
DOWNLOAD_DIR=/tmp

## FIXME
# The api.github.com json has alpha releases at the top so here we are trying to exclude names containing the word alpha
#RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq '[ .[] | select(.name|test("alpha.") | not) ] | .[0] | .name')
# Select name directly [v followed by dots and digits only], rather than by 'not alpha':
RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq '[ .[] | select(.name|test("v[[:digit:].]+$"))]|.[0] | .name')

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

# strip speech marks and preceding 'v'
VERSION=$(echo ${RELEASE} | tr -d '"' | tr -d "v")
echo $VERSION

## FIXME: Maake condition on alpha being in the RELEASE name.
#name	"v3.0.0-alpha.1" has the preceding 'v' removed and is split on '-' to give:
#VERSION=$(echo "${RELEASE}" | cut -c 2- | cut -f1 -d"-")
#RELEASE=$(echo "${RELEASE}" | cut -c 2- | cut -f2 -d"-")

# Delete these two when you've fixed the jq above
#VERSION=1.6.0
DEB_RELEASE=1

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

cat >./description-pak <<EOF
Little helper to run Rancher Lab's k3s in Docker.  k3s is the lightweight Kubernetes distribution.
EOF

cat >./checkinstall_it.sh <<EOF
#!/usr/bin/env bash

echo "ENTERING CHECKINSTALL"

BASE_URL='https://api.github.com/repos'
BASE_USER='rancher'
BASE_REPO='k3d'
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}" | tr '\r\n' ' ' | 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=$(date +%Y-%m-%d_)git
#VERSION="1.0" 
#RELEASE="git"
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=rancher-k3d \
	--pkgversion=${VERSION}\
	--pkgrelease=${DEB_RELEASE} \
	--pkgarch=amd64 \
	--pkggroup=admin \
	--pkglicense=LICENSE \
 	--pkgsource=${LATEST_URL} \
 	--maintainer=cyteen@ring-zero.co.uk \
 	--requires=${REQUIRES} \
 	-D \
 	bash ./install-it.sh
EOF

echo "${RELEASE}"

cat >./install-it.sh <<EOF
#!/usr/bin/env bash

#wget -c https://raw.githubusercontent.com/rancher/k3d/master/install.sh 
#sed -i '/^#\!.*/ s/.*/&\nset -x/' ./install.sh
#TAG=${RELEASE} bash install.sh

echo "Installing release ${RELEASE} "
curl -SsL http://github.com/rancher/k3d/releases/download/${RELEASE}/k3d-linux-amd64 -o /usr/local/bin/k3d
chmod +x /usr/local/bin/k3d
EOF

bash ./checkinstall_it.sh
