bin/tmp

83 lines
1.5 KiB
Plaintext
Raw Normal View History

2018-09-16 12:48:35 -05:00
#! /bin/bash
#
# tmp.bash
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
#
# Distributed under terms of the MIT license.
#
2018-10-04 16:20:38 -05:00
# Define variables
2018-09-16 12:48:35 -05:00
name="$(basename "$0" .sh)"
tmpdir="$(mktemp -d)"
2019-01-23 14:49:16 -06:00
2018-10-04 16:20:38 -05:00
# Verify them
2019-01-23 14:49:16 -06:00
[ -z "$tmpdir" ] && exit 2
2018-10-04 16:20:38 -05:00
# Define functions
2019-01-23 14:49:16 -06:00
log() {
[ -z "$1" ] && return 1
2018-11-22 02:43:48 -06:00
# shellcheck disable=1117
printf "%b: %s\n" \
"$name" \
"$1"
2018-09-16 12:48:35 -05:00
}
2019-01-23 14:49:16 -06:00
cleanup() {
[ -z "$tmpdir" ] && exit 2
log "Cleaning up \"$tmpdir\""
rm -rf "$tmpdir"
2018-10-04 16:20:38 -05:00
}
2019-01-23 14:49:16 -06:00
# Handle args
while getopts ":h" opt; do
case $opt in
h)
cat << EOF
Usage: $name [TEMPLATE]
Create a temporary directory, copying the folder [TEMPLATE] from
[XDG_TEMPLATES_DIR|~/Templates]/tmp, if it exists.
2019-01-23 14:49:16 -06:00
-h Show this help text
https://gitlab.com/rehashedsalt/bin
EOF
exit 0
;;
*)
2019-02-09 20:12:37 -06:00
log "Invaild argumenet: -$OPTARG"
log "See $name -h for help"
2019-01-23 14:49:16 -06:00
exit 1
;;
esac
done
# Copy in template directory
if [ -n "$1" ]; then
2019-02-10 12:20:05 -06:00
# Source in XDG dirs, if they exist
userdirs="${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs"
if [ -f "$userdirs" ]; then
source "$userdirs"
fi
unset userdirs
copydir="${XDG_TEMPLATES_DIR:-~/Templates}/tmp/$1"
if ! [ -d "$copydir" ]; then
2019-01-23 14:49:16 -06:00
log "Could not find template directory \"$copydir\""
exit 3
2019-01-23 14:49:16 -06:00
fi
if ! [ -r "$copydir" ]; then
log "Cannot read template directory \"$copydir\""
exit 4
fi
log "Using template \"$1\""
cp -r "$copydir"/* "$tmpdir"
2019-02-10 12:20:05 -06:00
unset copydir
2019-01-23 14:49:16 -06:00
fi
2018-10-04 16:20:38 -05:00
trap "cleanup" EXIT
# Do the do
2019-01-23 14:49:16 -06:00
log "This directory will be removed when this shell exits"
2018-09-16 12:48:35 -05:00
(
2019-01-23 14:49:16 -06:00
cd "$tmpdir" || exit 50
2018-11-22 02:43:48 -06:00
exec "$SHELL"
2018-09-16 12:48:35 -05:00
)