#!/bin/sh

# create local git, and create and push to remote git

set -e

login=debian@source.jones.dk
path="/srv/git/source.jones.dk/epfsug/diff"

# resolve options
eval set -- "$(getopt -s sh -o f -- "$@")"
while true; do case "$1" in -f) force=-f; shift;; --) shift; break;; esac; done

. /lib/lsb/init-functions

# add suffix unless already included
gitpath="$(dirname "$path")/$(basename "$path" .git).git"

log_action_begin_msg "Publish git"

# initialize local git
log_action_cont_msg "init"
if [ -d .git ]; then
	if [ -n "$force" ]; then
		log_warning_msg "purging"
		rm -rf .git
	else
		log_failure_msg "local git already exist (force with -f)"
		exit 1
	fi
fi
git init

# add scripts
log_action_cont_msg "add"
git add mk*; git commit -m "Include mk* scripts"

# create and populate remote git
log_action_cont_msg "push"
ssh "$login" git init --bare "$gitpath" \&\& cd "$gitpath" \&\& mv hooks/post-update{.sample,} \&\& touch git-daemon-export-ok
git push $force --all -u "$login:$gitpath" || { log_failure_msg "remote git already exist (force with -f)"; exit 1; }

log_action_end_msg $?