#!/bin/bash

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

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

sudo apt-get install python3-pbr

mkdir -p "${BUILD_DIR}" && cd "${BUILD_DIR}" || exit
git clone https://github.com/"${USER}"/"${PROJECT}" "${BUILD_DIR}"/"${PROJECT}"-"${RELEASE}"

cd "${BUILD_DIR}"/"${PROJECT}"-"${RELEASE}" || exit
echo "pwclient is a VCS-agnostic tool for interacting with Patchwork, the web-based patch tracking system." >description-pak
mkdir -p doc-pak

cat <<-EOF | sudo tee doc-pak/README >/dev/null
	pwclient reads configuration from the .pwclientrc file, located in your home directory (\$HOME or ~). Patchwork itself provides sample .pwclientrc files for projects at:

	/project/{projectName}/pwclientrc/

	[options]
	default = linux-rockchip

	[linux-wireless]
	backend = rest
	url = https://patchwork.kernel.org/api/

	[netdevbpf]
	backend = rest
	url = https://patchwork.kernel.org/api/

	[linux-rockchip]
	backend = rest
	url = https://patchwork.kernel.org/api/
	project = linux-rockchip
	# token =

EOF

#sudo checkinstall -y --fstrans=no pip3 install --user .
sudo checkinstall -y --fstrans=no sudo env "PBR_VERSION=${RELEASE}" python setup.py install

cat <<-EOF | sudo tee "${DEST}"/.pwclientrc >/dev/null
	[options]
	default = base

	[patchwork]
	backend = rest
	url = https://patchwork.ozlabs.org/api/

	# http://lists.infradead.org/mailman/listinfo/linux-rockchip
	[base]
	backend = rest
	url = https://patchwork.kernel.org/api/
	project = linux-rockchip
EOF
