#! /usr/bin/env sh main() { # Handle opts opts "$@" shift "$OPTS" unset OPTS # Show help [ "$OPT_HELP_ONLY" ] && usage && exit 0 if [ -d "$1" ] then printf "A directory already exists at %s\n" "$1" >&2 exit 1 fi if [ -f "$1" ] then printf "A file already exists at %s\n" "$1" >&2 exit 1 fi if [ -z "$TYPE" ] then # TODO: Calculate type from extension TYPE= fi if [ ! -f "$XDG_TEMPLATES_DIR/$TYPE" ] then printf "No template for %s\n" "$TYPE" >&2 touch "$1" return 0 fi cp -- "$XDG_TEMPLATES_DIR/$TYPE" "$1" } opts() { OPTS=0 while getopts ":ht:" opt do case "$opt" in h) OPT_HELP_ONLY=1 OPTS=$((OPTS + 1)) ;; t) TYPE=$OPTARG OPTS=$((OPTS + 2)) ;; *) printf "Invalid option passed: %s\n" "$OPTARG" >&2 ;; esac done unset opt } usage() { cat < Create a new file from a template, found in the \$XDG_TEMPLATES_DIR directory. Options: -h Show this help text and exit. -t Specify the template type to use. EOF } main "$@"