#!/bin/bash

if [ -z "$1" ]
then
	printf 'Repository: '
	read
	set "$REPLY"
fi

if [ ! -f "$1/HEAD" ]
then
	echo 'Not a repository!'
	exit 1
fi

set "${1##/}"

# If repo is in file
if echo -e "?^$1\$?" | ed -s /public_repos >/dev/null 2>&1
then
	echo "Already public!"
# If repo root dir is in file
elif echo -e "?^${1%%/*}?" | ed -s /public_repos >/dev/null 2>&1
then
	echo -e "?^${1%%/*}?a\n$1\n.\nw" | ed -s /public_repos
	echo "'$1' is now public!"
# Repo and repo root dir are not anywhere in file
else
	echo -e "\$a\n$1\n.\nw" | ed -s /public_repos
	echo "'$1' is now public!"
fi

echo "https://git.syndamia.com/$1"
