about summary refs log tree commit diff stats
path: root/dots.sh
diff options
context:
space:
mode:
authorCase Duckworth2024-05-30 12:47:11 -0500
committerCase Duckworth2024-05-30 12:47:11 -0500
commit22bc1e1879da0d99e5e1d79b10742b8cc9fd0521 (patch)
treecd813c6d8bfeebe83901b2d749ce97f1231dd547 /dots.sh
downloaddots-22bc1e1879da0d99e5e1d79b10742b8cc9fd0521.tar.gz
dots-22bc1e1879da0d99e5e1d79b10742b8cc9fd0521.zip
Initial commit
Diffstat (limited to 'dots.sh')
-rwxr-xr-xdots.sh81
1 files changed, 81 insertions, 0 deletions
diff --git a/dots.sh b/dots.sh new file mode 100755 index 0000000..5036e30 --- /dev/null +++ b/dots.sh
@@ -0,0 +1,81 @@
1#!/bin/sh
2
3QUIET=false
4NORUN=false
5FORCE=false
6DIR="$(dirname "$(realpath "$0")")"
7
8main() {
9 action=homify
10 while getopts hqnfdx OPT
11 do
12 case "$OPT" in
13 (h) usage; exit 0 ;;
14 (q) QUIET=true ;;
15 (n) NORUN=true ;;
16 (f) FORCE=true ;;
17 (d) action=dotify ;;
18 (x) set -x ;;
19 (*) usage; exit 1 ;;
20 esac
21 done
22 shift $((OPTIND - 1))
23 "$action" "$@"
24}
25
26usage() {
27 cat<<EOF
28Usage: $0 [-h] [-q] [-n] [-f] [-x] [-d] [FILE] [FIND_ARGS...]
29Flags:
30 -h Show this help and exit
31 -q Don't print what's happening
32 -n Just print what would happen
33 -f Force links (ln -f)
34 -x Trace program
35 -d Link from FILE to the dots dir
36 (Default: link from FILE to $HOME)
37 When linking to $HOME, add a dot at the front of FILE
38 When linking to dots, remove the dot from the front of FILE
39Parameters:
40 FILE File or directory to link.
41 If not given, link all the files in dots.
42 If not given along with '-d', ... I don't know what'll happen
43 FIND_ARGS
44 Extra arguments to pass to find.
45EOF
46}
47
48log() { $QUIET || echo >&2 "* $@"; }
49run() { log "$@"; $NORUN || "$@"; }
50
51mklink() { # mklink SOURCE DEST
52 # make hardlinks to DEST from SOURCE -- making directories
53 $NORUN || test -d "$(dirname "$2")" || run mkdir -p "$(dirname "$2")"
54 if ! test -f "$2"
55 then run ln "$1" "$2"
56 elif test -f "$2" && "$FORCE"
57 then run ln -f "$1" "$2"
58 else log "$2 exists: skipping"
59 fi
60}
61
62getfiles() {
63 d="$1"; shift
64 find "$d" -type f -a -not -name "$(basename "$0")" "$@"
65}
66
67homify() {
68 getfiles "$DIR" "$@" |
69 while read -r dot
70 do mklink "$dot" "$HOME/.${dot#$DIR/}"
71 done
72}
73
74dotify() {
75 getfiles "$@" |
76 while read -r file
77 do mklink "$file" "$DIR/${file#.}"
78 done
79}
80
81main "$@"