#!/bin/bash

USER="${1:?usage: $0 <user> <archive>}"
ARCHIVE_LOC="${2:?usage: $0 <user> <archive>}"

which github-backup > /dev/null 2>&1 || pip install github-backup

if [ -z "$GH_BACKUP_TOKEN" ];then
	echo 'missing $GH_BACKUP_TOKEN...'
	exit 1
fi

_tmp=$(mktemp -d -p "/run/user/$(id -u)" -t "github-backup-XXXXX")
if [ "$?" != 0 ];then exit 1;fi

trap "rm -rf $_tmp" EXIT
echo "using '$_tmp'..."

github-backup \
	--token "$GH_BACKUP_TOKEN" \
	--starred \
	--followers \
	--following \
	--issues \
	--issue-comments \
	--repositories \
	--wikis \
	--gists \
	--starred-gists \
	--private \
	--output-directory "$_tmp" "$USER"

tar -czvf "$ARCHIVE_LOC" -C "$_tmp" .
