various fixes
[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
15
16 shopt -s nullglob
17 shopt -s extglob
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 sysv s cp -R ${fs_files[@]} /
51 fi
52 if [[ -e $dir/subdir_files ]]; then
53 subdir-link-r $dir/subdir_files
54 fi
55 local x=( $dir/!(subdir_files|filesystem|machine_specific) )
56 (( ${#x[@]} >= 1 )) || continue
57 sysv lnf ${x[@]} ~
58 done
59 }
60
61 all_dirs=({/a/c,/p/c}{,/machine_specific/$HOSTNAME})
62 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
63 case $USER in
64 ian)
65 # p needs to go first so .ssh link is created, then config link inside it
66 common-file-setup ${all_dirs[@]}
67 sudo -u traci "$BASH_SOURCE"
68 ;;
69 traci)
70 common-file-setup ${c_dirs[@]}
71 ;;
72 *)
73 echo "$0: error: unexpected user"; exit 1
74 ;;
75 esac