#!/bin/bash

# Tokyo Night gtk   https://www.pling.com/p/1681315/
# Tokyo Night shell https://www.pling.com/p/1681470/
# Tokyo Night Icons https://www.pling.com/p/1681475/

DEST=${1:-${HOME}}

sudo apt install -y sassc libsass1
cd /var/tmp/

SYSTEM_HOME=/usr/share/themes/

USER='Fausto-Korpsvart'
PROJECT='Tokyo-Night-GTK-Theme'
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
BUILD_DIR="/var/tmp/tokyonight-gtk-theme"

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

# make a containing directory
mkdir -p "${BUILD_DIR}" && cd "${BUILD_DIR}" || exit

cat <<-EOF | sudo tee ./description-pak >/dev/null
	 A GTK theme based on the colours of Folke's great theme: Tokyonight for Neovim, 
	 the VinceLiuice's awesome Magnetic GTK theme and the creativity of Gusbemacbe's
	 Suru Plus Icon Theme.
EOF

mkdir -p doc-pak

cat <<-EOF | sudo tee doc-pak/README >/dev/null
	 Tokyonight GTK Theme

	 A GTK theme based on the colours of Folke's great theme: Tokyonight for Neovim,
	 the VinceLiuice's awesome: Magnetic GTK theme and the creativity of Gusbemacbe's 
	 Suru Plus Icon Theme. Great to combine in your Gnome Desktop Environment and TWMs
	 like: XmonadWM, AwesomeWM, BSPWM, etc... With support also for the desktop 
	 environments Cinnamon and XFCE.
EOF

cat <<-'EOF' | sudo tee ./checkinstall_it.sh >/dev/null
	 echo "ENTERING CHECKINSTALL"

	 BASE_URL='https://api.github.com/repos'
	 BASE_USER='Fausto-Korpsvart'
	 BASE_REPO='Tokyo-Night-GTK-Theme'
	 LICENSE_PAGE_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/license

	 git clone --depth 1 https://github.com/"${BASE_USER}/${BASE_REPO}"

	 echo "LATEST_URL: " "${LATEST_URL}"

	 VERSION=$(date +%Y-%m-%d_)git
	 #VERSION="1.0" 
	 RELEASE="git"
	 REQUIRES="gtk2-engines-murrine"

	 # 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 \
	    --exclude=/root/.sudo_as_admin_successful \
	    --pkgname=tokyonight-gtk-theme \
	    --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 | sudo tee ./install.sh >/dev/null
	 # variants: [default|purple|pink|red|orange|yellow|green|teal|grey|all]
	 pushd ${PROJECT}/theme || exit
	    bash ./build.sh \
	    bash ./install.sh \
	      --dest ${SYSTEM_HOME} \
	      --name Tokyonight \
	      --theme all \
	      --color dark \
	      --size standard \
	      --libadwaita \
	      --tweaks outline
	  popd
EOF

bash ./checkinstall_it.sh
