working
[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 shopt -s nullglob
14
15 # if we make a link back to the root, traversing the subdirs stops.
16 # This makes it so we can do subdir directories. Must call with absolute
17 # paths.
18
19 subdir-link-r() {
20 local root="$1"
21 local targets=()
22 if [[ $2 ]]; then
23 targets=( "$2"/* )
24 else
25 for f in "$1"/*; do
26 [[ -d $f ]] && targets+=("$f") ||:
27 done
28 fi
29 local below="$( readlink -f "$root/..")"
30 for path in "${targets[@]}"; do
31 local fullpath="$(readlink -f "$path")"
32 #e $fullpath $below # debug
33 if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
34 if [[ $dir == /p/* ]]; then
35 homes=(/home/ian)
36 else
37 homes=(/home/*)
38 fi
39 for user in ${homes[@]}; do
40 sysv lnf -T "$path" "$user/${path#$root/}"
41 done
42 elif [[ -d "$path" ]]; then
43 subdir-link-r "$root" "$path"
44 fi
45 done
46 }
47
48 common-file-setup() {
49 if [[ -e $1/subdir_files ]]; then
50 subdir-link-r $1/subdir_files
51 fi
52 local x=( $1/!(subdir_files|filesystem) )
53 (( ${#x[@]} >= 1 )) || return 0
54 sysv lnf ${x[@]} ~
55 # as of this writing, it doesn't exist in /a/c, but I've used it in the past.
56 if [[ -e $1/filesystem ]]; then
57 sysv s cp -R $1/filesystem/* /
58 fi
59 }
60
61 # p needs to go first so .ssh link is created, then config link inside it
62 for dir in {/a/c,/p/c}{,/machine_specific/$HOSTNAME}; do
63 common-file-setup $dir
64 done