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