#!/usr/bin/env bash

cd /var/tmp

USER=ckampfe
PROJECT=russ
PRERELEASE=false
RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
# VERSION=$(echo "${RELEASE}" | cut -c 2- | cut -f1 -d"-")
VERSION=$(date +%Y-%m-%d_)git
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 <<EOF | tee ./description-pak
Russ is a TUI RSS/Atom reader with vim-like controls and a local-first, offline-first focus.
EOF

cat <<EOF | tee ./checkinstall_it.sh

echo "ENTERING CHECKINSTALL"

BASE_URL='https://api.github.com/repos'
BASE_USER=''ckampfe
BASE_REPO='russ'
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=$(date +%Y-%m-%d_)git
VERSION="1.0" 
RELEASE="git"
REQUIRES="libxcb-shape0-dev,libxcb-xfixes0-dev"

# 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=russ \
	--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 <<EOF | tee ./install.sh
cargo install russ --git https://github.com/ckampfe/russ
EOF

bash ./checkinstall_it.sh
