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)"
|
2020-10-30 00:00:44 -05:00
|
|
|
tmpdirtemplate="XXXXXX"
|
2019-01-23 14:49:16 -06:00
|
|
|
|
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
|
2019-02-10 12:29:23 -06:00
|
|
|
[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
|
|
|
|
|
2020-10-30 00:00:44 -05:00
|
|
|
# Validate $1
|
2019-01-23 14:49:16 -06:00
|
|
|
if [ -n "$1" ]; then
|
2019-02-10 12:20:05 -06:00
|
|
|
# Source in XDG dirs, if they exist
|
2020-10-29 23:36:47 -05:00
|
|
|
userdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
|
2019-02-10 12:20:05 -06:00
|
|
|
if [ -f "$userdirs" ]; then
|
|
|
|
source "$userdirs"
|
|
|
|
fi
|
|
|
|
unset userdirs
|
2020-10-29 23:36:47 -05:00
|
|
|
copydir="${XDG_TEMPLATES_DIR:-$HOME/Templates}/.tmp/$1"
|
2019-02-10 12:29:23 -06:00
|
|
|
if ! [ -d "$copydir" ]; then
|
2019-01-23 14:49:16 -06:00
|
|
|
log "Could not find template directory \"$copydir\""
|
2019-02-10 12:29:23 -06:00
|
|
|
exit 3
|
2019-01-23 14:49:16 -06:00
|
|
|
fi
|
2019-02-10 12:29:23 -06:00
|
|
|
if ! [ -r "$copydir" ]; then
|
|
|
|
log "Cannot read template directory \"$copydir\""
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
log "Using template \"$1\""
|
2020-10-30 00:00:44 -05:00
|
|
|
tmpdirtemplate="$1.$tmpdirtemplate"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Make our directory
|
|
|
|
tmpdir="$(mktemp -dt "tmp.$tmpdirtemplate")"
|
2020-12-21 01:50:49 -06:00
|
|
|
[ -z "$tmpdir" ] && exit 2
|
2020-10-30 00:00:44 -05:00
|
|
|
|
|
|
|
# Copy in template directory
|
|
|
|
if [ -n "$1" ] && [ -n "$copydir" ] && [ -n "$tmpdir" ]; then
|
2019-02-10 12:29:23 -06:00
|
|
|
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
|
|
|
)
|
|
|
|
|