Rewrite dot-put script

This commit is contained in:
ItsDrike 2024-02-17 23:11:38 +01:00
parent 05e61f18a1
commit b29dfb9af6
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

@ -4,7 +4,24 @@ set -euo pipefail
dotdir="$HOME/dots" dotdir="$HOME/dots"
dothome="$dotdir/home" dothome="$dotdir/home"
dotroot="$dotdir/root" dotroot="$dotdir/root"
syspath="$(realpath "$1")"
usage() {
echo "$0"
echo
echo "Usage: $0 [options] <file> <file...>
Options:
-h, --help Print this usage message.
Positional arguments:
file Path to be copied to dotfiles.
Details:
A script to quickly copy specified files to dotfile directory.
"
}
dotcopy() {
syspath="$1"
if [[ "$syspath" == "$HOME"* ]]; then if [[ "$syspath" == "$HOME"* ]]; then
dotpath="${syspath/#$HOME/$dothome}" dotpath="${syspath/#$HOME/$dothome}"
@ -12,4 +29,36 @@ else
dotpath="$dotroot$syspath" dotpath="$dotroot$syspath"
fi fi
# Ensure the destination directory exists
mkdir -p "$(dirname "$dotpath")"
cp -vi "$syspath" "$dotpath" cp -vi "$syspath" "$dotpath"
}
getopt=$(getopt \
--longoptions=help \
--options=h \
-- "$@") || exit 1
eval set -- "${getopt}"
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
exit 0
;;
--)
shift 1
break
;;
esac
done
if [ $# -eq 0 ]; then
echo "No paths specified" >&2
exit 1
fi
for arg in "$@"; do
dotcopy "$arg"
done