#!/bin/bash

# DEST=${1:-/etc/skel}
USER=getpatchwork
PROJECT=git-pw
# RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/releases?per_page=5" | \
#	  jq -r '.[0] | .tag_name')
RELEASE=$(lastversion ${USER}/${PROJECT})
BUILD_DIR=/var/tmp/build_git-pw_${RELEASE}

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

sudo apt-get install python3-requests python3-click python3-pbr \
	python3-arrow python3-tabulate python3-yaml

mkdir -p "${BUILD_DIR}" && cd "${BUILD_DIR}" || exit

echo "git clone -b ${RELEASE} https://github.com/${USER}/${PROJECT} ${BUILD_DIR}/${PROJECT}-${RELEASE}"
git clone -b "${RELEASE}" https://github.com/"${USER}"/"${PROJECT}" "${BUILD_DIR}"/"${PROJECT}"-"${RELEASE}"

echo "cd ${BUILD_DIR}/${PROJECT}-${RELEASE}"
cd "${BUILD_DIR}"/"${PROJECT}"-"${RELEASE}" || exit
echo "git-pw is a tool for integrating Git with Patchwork, the web-based patch tracking system." >description-pak

mkdir -p doc-pak

cat <<EOF | sudo tee doc-pak/README >/dev/null
	git-pw is a tool for integrating Git with Patchwork.

	git-pw can interact with individual patches, complete patch series, and customized bundles. 
	The three major subcommands are patch, bundle, and series.

	The git-pw utility is a wrapper which makes REST calls to the Patchwork service. 
	To use git-pw, you must set up your environment by configuring your Patchwork server URL and 
	either an API token or a username and password. To configure the server URL, run:

	git config pw.server 'https://patchwork.kernel.org/api/1.2'
	git config pw.project 'linux-rockchip'
	#git config pw.token ''
	#git config pw.password ''
	#git config pw.states ''

	git-pw --help

	See: https://patchwork.readthedocs.io/projects/git-pw/en/latest/usage/
EOF

#sudo checkinstall -y --fstrans=no pip3 install --user .
sudo checkinstall -y --fstrans=no sudo python setup.py install
