#!/bin/sh
# Create GIT repository and import content of a sandbox
# Copyright (C) 2018 FARGOS Development, LLC
# $Id$

gitHost="${GIT_HOST}"
gitPath="${GIT_PATH}"
gitRepo=""

dirList=""
sawImport=0

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [--host gitHostURL] [--path repositoryPath] --import [dirName ...]\n" "${0}" >&2
		printf "  --host defaults to GIT_HOST, currently=\"%s\"\n" "${gitHost}"
		printf "  --path defaults to GIT_PATH, currently=\"%s\"\n" "${gitPath}"
		printf "  --import = mandatory flag\n" >&2
		exit 1
		;;
	--host)
		gitHost="${2}"
		shift
		;;
	--path)
		gitPath="${2}"
		shift
		;;
	--repo)
		gitRepo="${2}"
		shift
		;;
	--import)
		sawImport=1
		;;
	-*)
		printf "%s: unrecognized option: \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		dirList="${dirList} ${1}"
	esac
	shift
done
if test -z "${STS}"
then
	printf "%s: not in a sandbox\n" "${0}" >&2
	exit 1
fi
if test "${sawImport}" -eq 0
then
	printf "%s: mandatory --import argument missing\n" "${0}" >&2
	exit 1
fi
cd ${STS}
git init
git remote add origin "${gitHost}${gitPath}${gitRepo:+/}${gitRepo}"

if test -z "${dirList}"
then
	dirProtoList=`echo *`
	for d in ${dirProtoList}
	do
		if test -d ${d}
		then
			dirList="${dirList} ${d}"
		else
			printf "Cannot import non-directory: \"%s\"\n" "${d}" >&2
		fi
	done
fi
for d in ${dirList}
do
	git add ${d}
done
git commit

# now push to remote repository
git push origin master
