bash 4.4 fix
[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 echo "$@"
9 "$@"
10 }
11
12
13 #
14
15
16 shopt -s nullglob
17 shopt -s extglob # note, already set with bash -l
18
19 # if we make a link back to the root, traversing the subdirs stops.
20 # This makes it so we can do subdir directories. Must call with absolute
21 # paths.
22
23 subdir-link-r() {
24 local root="$1"
25 local targets=()
26 if [[ $2 ]]; then
27 targets=( "$2"/!(.git|..|.) )
28 else
29 for f in "$1"/!(.git|..|.); do
30 [[ -d $f ]] && targets+=("$f") ||:
31 done
32 fi
33 local below="$( readlink -f "$root/..")"
34 for path in "${targets[@]}"; do
35 local fullpath="$(readlink -f "$path")"
36 #e $fullpath $below # debug
37 if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
38 sysv lnf -T "$path" "$HOME/${path#$root/}"
39 elif [[ -d "$path" ]]; then
40 subdir-link-r "$root" "$path"
41 fi
42 done
43 }
44
45 common-file-setup() {
46 local dir fs_files x
47 for dir in "$@"; do
48 fs_files=( $dir/filesystem/* )
49 if [[ -e ${fs_files[0]} && $USER == ian ]]; then
50 # note, symlinks get resolved, not copied.
51 sysv s cp -RL ${fs_files[@]} /
52 fi
53 if [[ -e $dir/subdir_files ]]; then
54 subdir-link-r $dir/subdir_files
55 fi
56 local x=( $dir/!(subdir_files|filesystem|machine_specific) )
57 (( ${#x[@]} >= 1 )) || continue
58 sysv lnf ${x[@]} ~
59 done
60 }
61
62 all_dirs=({/a/c,/p/c}{,/machine_specific/$HOSTNAME})
63 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
64 case $USER in
65 ian)
66 # p needs to go first so .ssh link is created, then config link inside it
67 common-file-setup ${all_dirs[@]}
68 sudo -u traci "$BASH_SOURCE"
69 ;;
70 traci)
71 common-file-setup ${c_dirs[@]}
72 ;;
73 *)
74 echo "$0: error: unexpected user"; exit 1
75 ;;
76 esac