#!/usr/bin/env zsh

emulate -L zsh

if [[ ${1} == '--help' || ${1} == '-h' || ${1} == '' ]]; then
    echo "Usage: ${0} [--help|-h] [reference]"
    return 0
fi

if [[ -f .gitignore ]]; then
    echo '.gitignore already exists'
    return 1
fi

local TEMP="$(mktemp)"
if curl --fail --silent "https://raw.githubusercontent.com/github/gitignore/main/${1}.gitignore" > "${TEMP}"; then
   mv "${TEMP}" ".gitignore"
elif curl --fail --silent "https://raw.githubusercontent.com/github/gitignore/main/${(C)1}.gitignore" > "${TEMP}"; then
    mv "${TEMP}" ".gitignore"
else
    echo "Could not get ${1}"
    return 2
fi

# Uncomment things I generally prefer uncommented
sed -i -E 's% *# *(.idea/|poetry.lock|Pipfile.lock|.python-version|pdm.lock)%\1%g' ".gitignore"
echo ".gitignore created"