touch with templates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tmpl/tmpl.sh

87 lines
2.2 KiB

3 years ago
#!/bin/sh
# MIT License
# Copyright (c) 2021 Alessandro Mauri
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
usage() {
printf 'tmpl [-hf] [-c dir] filename\n'
printf '\t-h: print this message\n'
printf '\t-f: force override\n'
printf '\t-c dir: change template directory location\n'
}
unset DIR
FORCE=false
while getopts 'hfc:' c
do
case "$c" in
h) usage; exit 0 ;;
f) FORCE=true ;;
c) DIR="$OPTARG" ;;
*) ;;
esac
done
shift $((OPTIND - 1))
if ! [ "$1" ]; then
echo "Not enough arguments"
usage
exit 1
fi
# get the right directory
if [ "$DIR" ]; then
if ! [ -d "$DIR" ]; then
echo "Cannot find template directory"
exit 1
fi
elif ! [ -d "$DIR" ]; then
DIR="$HOME"/.config/tmpl
if ! [ -d "$DIR" ]; then
DIR="$HOME"/.tmpl
if ! [ -d "$DIR" ]; then
echo "Cannot find template directory"
exit 1
fi
fi
fi
tmp="$(mktemp)"
find "$DIR" -maxdepth 1 -type f > "$tmp"
iext="${1##*.}"
while IFS= read -r file; do
fn="${file##*/}"
ext="${fn##*.}"
if [ "$fn" = "$1" ] || [ "$ext" = "$iext" ]; then
if [ $FORCE ]; then
cp "$file" "$1"
else
cp -i "$file" "$1"
fi
exit 0
fi
done < "$tmp"
rm "$tmp"
echo "No template found, touching"
touch "$1"
exit 0