user improvements, refactoring
[distro-setup] / conflink
1 #!/bin/bash -l
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
5
6
7 sysv() {
8 e "$@"
9 "$@"
10 }
11
12
13 #
14 if [[ -e /root/.unison && ! -L /root/.unison ]]; then
15 d=/q/p/c/machine_specific/$host/.unison
16 mkdir -p $d
17 mv -f /root/.unison/* $d
18 fi
19
20
21 shopt -s nullglob
22 shopt -s extglob
23
24 s lnf -T /q/p /p
25 s lnf -T /a/bin /b
26
27
28 # if we make a link back to the root, traversing the subdirs stops.
29 # This makes it so we can do subdir directories. Must call with absolute
30 # paths.
31
32 subdir-link-r() {
33 local root="$1"
34 local targets=()
35 if [[ $2 ]]; then
36 targets=( "$2"/!(.git) )
37 else
38 for f in "$1"/!(.git); do
39 [[ -d $f ]] && targets+=("$f") ||:
40 done
41 fi
42 local below="$( readlink -f "$root/..")"
43 for path in "${targets[@]}"; do
44 local fullpath="$(readlink -f "$path")"
45 #e $fullpath $below # debug
46 if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
47 sysv lnf -T "$path" "$HOME/${path#$root/}"
48 elif [[ -d "$path" ]]; then
49 subdir-link-r "$root" "$path"
50 fi
51 done
52 }
53
54 common-file-setup() {
55 for dir in "$@"; do
56 if [[ -e $dir/subdir_files ]]; then
57 subdir-link-r $dir/subdir_files
58 fi
59 local x=( $dir/!(subdir_files|filesystem) )
60 (( ${#x[@]} >= 1 )) || return 0
61 sysv lnf ${x[@]} ~
62 # as of this writing, it doesn't exist in /a/c, but I've used it in the past.
63 if [[ -e $dir/filesystem && $USER == ian ]]; then
64 sysv s cp -R $dir/filesystem/* /
65 fi
66 done
67 }
68
69 link-dirs() {
70 for dir in "$@"; do
71 common-file-setup $dir
72 common-file-setup $dir/machine_specific/$HOSTNAME
73 done
74 }
75
76 all_dirs=({/a/c,/p/c}{,/machine_specific/$HOSTNAME})
77 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
78 case $USER in
79 ian)
80 # p needs to go first so .ssh link is created, then config link inside it
81 common-file-setup $dir ${all_dirs[@]}
82 sudo -u traci "$BASH_SOURCE"
83 ;;
84 traci)
85 common-file-setup ${c_dirs[@]}
86 ;;
87 root)
88 if ! $ROOTCMD getent passwd ian; then
89 common-file-setup ${all_dirs[@]}
90 else
91 echo "$0: error: unexpected user"; exit 1
92 fi
93 ;;
94 *)
95 echo "$0: error: unexpected user"; exit 1
96 ;;
97 esac