#/usr/bin/env bash

ARCH=$(dpkg --print-architecture)
if [[ ${ARCH} == "armhf" ]]; then ARCH="arm-v7"; fi
USER="zunit-zsh"
PROJECT="zunit"
PACKAGE="zsh-zunit"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq '[ .[] | select(.name|test("v[[:digit:].]+$"))]|.[0] | .name')
DEB_VERSION=$(echo "${VERSION}" | tr -d "\"" | tr -d "v") # strip the preceding 'v'
#VERSION="1.0.0"
#ARCH=$(dpkg --print-architecture)
MACHINE=$(uname -m)
OS=$(uname -s)
# DEBEMAIL="person@company.tld"
# DEBFULLNAME="Testy McTester"
# DESCRIPTION="."
# LONG_DESCRIPTION=" ."

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

EXCLUDES="/root/.wget-hsts"

REQUIRES="zsh"
BUILD_DIR=/var/tmp/build_zunit

## Checkinstall variables - see defaults in /etc/checkinstallrc
# DOC_DIR=/usr/share/doc

# make a new temporary directory for this use to avoid permission issues.
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
#-------------------------------------------------------------------------------

## Create and populate the install scripts and documentation for checkinstall
cat <<-EOF | tee ${BUILD_DIR}/description-pak
ZUnit is a powerful unit testing framework for ZSH
EOF

# | preinstall-pak  | Run BEFORE the package is INSTALLED  |
cat <<-EOF | tee ${BUILD_DIR}/preinstall-pak
#!/usr/bin/env bash
EOF

# | postinstall-pak | Run AFTER the package is INSTALLED   |
cat <<-EOF | tee ${BUILD_DIR}/postinstall-pak
#!/usr/bin/env bash
EOF

# | preremove-pak   | Run BEFORE the package is REMOVED    |
cat <<-EOF | tee ${BUILD_DIR}/preremove-pak
#!/usr/bin/env bash
EOF

# | postremove-pak  | Run AFTER the package is REMOVED
cat <<-EOF | tee ${BUILD_DIR}/postremove-pak
#!/usr/bin/env bash
EOF

## Make the doc-pak directory for README, INSTALL, COPYING, Changelog, TODO, CREDITS
mkdir -p ${BUILD_DIR}/doc-pak
#cp README INSTALL COPYING Changelog TODO CREDITS doc-pak

cat <<-EOF | tee ${BUILD_DIR}/doc-pak/README
EOF

cat <<-FOE | tee ${BUILD_DIR}/checkinstall_it.sh

## Checkinstall variables - see defaults in /etc/checkinstallrc
DOC_DIR=/usr/share/doc

# make a new temporary directory for this use to avoid permission issues.
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
mkdir -p '${BASE_TMP_DIR}'


# do your work
checkinstall -y --fstrans \
	--pkgname=${PACKAGE} \
	--pkgversion=${DEB_VERSION}\
	--pkgrelease=${RELEASE} \
	--pkgarch=${ARCH} \
	--pkggroup=admin \
	--pkglicense=LICENSE \
 	--pkgsource=${LATEST_URL} \
 	--maintainer=cyteen@ring-zero.co.uk \
 	--requires=${REQUIRES} \
	--recommends=${RECOMENDS} \
	--suggests=${SUGGESTS} \
	--exclude=${EXCLUDES} \
 	-D \
 	bash ${BUILD_DIR}/install.sh
FOE

echo "Writing install.sh to: ${BUILD_DIR}/install.sh"
cat <<-FOE | tee ${BUILD_DIR}/install.sh
#!/usr/bin/env bash
set -e
#set -x

# Install revolver
INSTALL_DIR=/usr/local/bin
curl -L https://raw.githubusercontent.com/molovo/revolver/master/revolver > ${INSTALL_DIR}/revolver

chmod +x ${INSTALL_DIR}/revolver

# Build ZUnit from source
git clone https://github.com/zunit-zsh/zunit ${BUILD_DIR}/zunit
cd ${BUILD_DIR}/zunit
./build.zsh

# Copy zunit into $path
cp zunit ${INSTALL_DIR}
chmod +x ${INSTALL_DIR}/zunit

# Optional, copy ZUnit ZSH completion into $fpath
cp zunit.zsh-completion "${fpath[1]}/_zu
FOE

bash ${BUILD_DIR}/checkinstall_it.sh
