#!/bin/bash
#emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- 
#ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
#   See COPYING file distributed along with the datalad package for the
#   copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# DEPRECATED!!!!!  Kept for references
#
set -e
#set -x
set -u

GH_ORG=datalad
GH_PREFIX=testrepo--

topdir=$(dirname `which $0 | xargs readlink -f `)/../..
topdir=$(readlink -f $topdir)
reposdir=$topdir/datalad/tests/testrepos

# So we could use special custom remotes
export PATH=$topdir/bin:$PATH PYTHONPATH=$topdir

flavor="$1"
name="$2"

# Repo specific variables
descr="datalad test repo $flavor/$name"
repodir=$reposdir/$flavor/$name
ghrepo=${GH_ORG}/${GH_PREFIX}$flavor--$name

# Information about the system
git_version=$(git --version | sed -e 's,.* \([0-9]\),\1,g')
annex_version=$(dpkg -l git-annex | awk '/^ii/{print $3;}')

_info() {
	echo "I: $@"
}

create_info_file() {
	cat >| $repodir/INFO.txt <<EOF
git: $git_version
annex: $annex_version
EOF
	git add $repodir/INFO.txt
}

rm_repo() {
	_info "removing $repodir"
	chmod +w -R $repodir/.git  # inefficient but for us sufficient
	rm -rf $repodir
}

create_repo() {
	_info "creating $repodir"
	mkdir -p $repodir
	cd $repodir
	git init
	git annex init $descr
}

create_github_repo() {
	_info "creating github repo"
	cd $repodir
	# it wouldn't delete or complain in exit code if exists already
	hub create -d "$descr (otherwise userless)" $ghrepo
	git remote | grep -q '^origin' || git remote add origin https://github.com/$ghrepo
	git push --set-upstream --all -f origin
}

flavor_basic() {
	create_repo
	create_info_file
	echo "123" > test.dat
	git add test.dat
	git commit -m "Adding a basic INFO file and rudimentary load file for annex testing"
	create_github_repo
#	cp test.dat test-annex.dat
#	git annex add test-annex.dat
	git annex addurl --file=test-annex.dat https://raw.githubusercontent.com/$ghrepo/master/test.dat
	git commit -m "Adding a rudimentary git-annex load file"
	git annex drop test-annex.dat # since available from github
	git push --all origin # and push again
}

initremote_archive() {
	git annex initremote annexed-archives \
	    encryption=none type=external externaltype=dl+archive
}

flavor_archive() {
	create_repo
	initremote_archive
	create_info_file
	mkdir -p d; echo "123" > d/test.dat; tar -czf d.tar.gz d;
    mv d/test.dat test2.dat; rm -rf d;
	git annex add d.tar.gz
	git commit -m "Added tarball"
	key=$(git annex lookupkey d.tar.gz)
	git annex add test2.dat
	git commit -m "Added the load file"
	git annex addurl --file test2.dat dl+archive:$key/d/test.dat
	_info "Added the dl+archive URL, committing"
	#git commit -m "Added a url for the file"
	git annex drop --force test2.dat # TODO -- should work without force
	git annex get test2.dat
}

if [ -e $repodir ]; then
	# TODO -- ask
	rm_repo
fi

register_repo_submodule() {
	cd $reposdir
	_info "registering git submodule"
	# TODO: verify if not registered already
	#git submodule ...
	git submodule add --force https://github.com/$ghrepo ./$flavor/$name && \
		git commit -m "Added test $flavor/$name" -a && \
		git push origin
}

# cd datalad/tests/testrepos
# hub create -d "Super-submodule collating test repositories for datalad" datalad/testrepos

eval flavor_$flavor

#register_repo_submodule
