#!/bin/bash

usage () {
    echo "repohere"
    echo "arguments:"
    echo "-d    use 'devel' for initial branch"
    echo "-m|-2 use 'main' for initial branch"
    echo "-M|-1 use 'master' for the initial branch"
    exit 0
}

branch=
case $1 in
    -d*) branch=devel ;;
    -m*|-2) branch=main ;;
    -M*|-1) branch=master ;;
    -h) usage ;;
esac
if [[ ! $branch ]]; then
    # User didn't pass a branch arg -- ask interactively
    select branch in master main devel; do
        break
    done
fi

if [[ $branch == devel ]]; then
    initial_tag="initial_d"
else
    initial_tag="initial"
fi


git_is_v1=
git_version=$(git version | cut -d' ' -f3)
if [[ $git_version = 1.* ]]; then
    git_is_v1=true
fi

set -e
if [[ $git_is_v1 ]]; then
    # git v1 does not support --initial-branch
    git init -q
else
    git init -q --initial-branch "$branch"
fi

if [[ $(whoami) =~ ms|matyas|matya ]]; then
    git config --local user.name  "Matyas Selmeci"
    git config --local user.email "mselmeci@wisc.edu"
elif [[ $(whoami) =~ mat|me|sysop ]]; then
    git config --local user.name  "Matyas Selmeci"
    git config --local user.email "matyasbot@gmail.com"
else
    git config --local user.email "$(whoami)@$(hostname -f)"
fi
git config --get user.email

touch .gitignore .git-blame-ignore-list
git add . .gitignore .git-blame-ignore-list
git commit -qm initial
if [[ $git_is_v1 && $branch != master ]]; then
    git branch -m "$branch"
fi
git tag "$initial_tag"
git config --local blame.ignoreRevsFile .git-blame-ignore-list

echo "done"
if command -v pre-commit &>/dev/null; then
    echo "pre-commit is available; consider adding a pre-commit-config"
fi

