diff options
author | Case Duckworth | 2024-05-30 12:47:11 -0500 |
---|---|---|
committer | Case Duckworth | 2024-05-30 12:47:11 -0500 |
commit | 22bc1e1879da0d99e5e1d79b10742b8cc9fd0521 (patch) | |
tree | cd813c6d8bfeebe83901b2d749ce97f1231dd547 /dots.sh | |
download | dots-22bc1e1879da0d99e5e1d79b10742b8cc9fd0521.tar.gz dots-22bc1e1879da0d99e5e1d79b10742b8cc9fd0521.zip |
Initial commit
Diffstat (limited to 'dots.sh')
-rwxr-xr-x | dots.sh | 81 |
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 | |||
3 | QUIET=false | ||
4 | NORUN=false | ||
5 | FORCE=false | ||
6 | DIR="$(dirname "$(realpath "$0")")" | ||
7 | |||
8 | main() { | ||
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 | |||
26 | usage() { | ||
27 | cat<<EOF | ||
28 | Usage: $0 [-h] [-q] [-n] [-f] [-x] [-d] [FILE] [FIND_ARGS...] | ||
29 | Flags: | ||
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 | ||
39 | Parameters: | ||
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. | ||
45 | EOF | ||
46 | } | ||
47 | |||
48 | log() { $QUIET || echo >&2 "* $@"; } | ||
49 | run() { log "$@"; $NORUN || "$@"; } | ||
50 | |||
51 | mklink() { # 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 | |||
62 | getfiles() { | ||
63 | d="$1"; shift | ||
64 | find "$d" -type f -a -not -name "$(basename "$0")" "$@" | ||
65 | } | ||
66 | |||
67 | homify() { | ||
68 | getfiles "$DIR" "$@" | | ||
69 | while read -r dot | ||
70 | do mklink "$dot" "$HOME/.${dot#$DIR/}" | ||
71 | done | ||
72 | } | ||
73 | |||
74 | dotify() { | ||
75 | getfiles "$@" | | ||
76 | while read -r file | ||
77 | do mklink "$file" "$DIR/${file#.}" | ||
78 | done | ||
79 | } | ||
80 | |||
81 | main "$@" | ||