use proper path
[zsh.git] / .zsh / functions / udh
1 # -*- mode:sh -*-
2
3 SPECBASE=${HOME}/.dotfiles
4 CRONBASE=${HOME}/.crontabs
5
6 if [ "$ZDOTDIR" = "" ]; then
7 echo "Sorry, need to run within a configured zsh environment"
8 return
9 fi
10 mr update
11
12 # Now we check if we have something specific for this host, that we need
13 # to link into the homedir
14
15 typeset -a specdirs
16 specdirs=(
17 # "${SPECBASE}/net:$DOMAIN"
18 # "${SPECBASE}/host:$HOST"
19 # "${SPECBASE}/sys:$OSNAME"
20 # "${SPECBASE}/distri:$DISTRI"
21 # "${SPECBASE}/user:$USER"
22 # "${SPECBASE}/user:$SUDO_USER"
23 "${SPECBASE}/net:$DOMAIN/host:$HOST"
24 "${SPECBASE}/net:$DOMAIN/sys:$OSNAME"
25 "${SPECBASE}/net:$DOMAIN/distri:$DISTRI"
26 "${SPECBASE}/net:$DOMAIN/user:$USER"
27 "${SPECBASE}/net:$DOMAIN/user:$SUDO_USER"
28 "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
29 "${SPECBASE}/net:$DOMAIN/host:$HOST/distri:$DISTRI"
30 "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$USER"
31 "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$SUDO_USER"
32 "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
33 "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/distri:$DISTRI"
34 "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$USER"
35 "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
36 "${SPECBASE}/host:$HOST/sys:$OSNAME"
37 "${SPECBASE}/host:$HOST/distri:$DISTRI"
38 "${SPECBASE}/host:$HOST/user:$USER"
39 "${SPECBASE}/host:$HOST/user:$SUDO_USER"
40 "${SPECBASE}/host:$HOST/sys:$OSNAME/distri:$DISTRI"
41 "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$USER"
42 "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
43 )
44
45 typeset -a workdirs
46 for entry in $specdirs[@]; do
47 for file in ${entry}/**/*(N.); do
48 workdirs+=$file
49 done
50 done
51 typeset -U workdirs
52
53 # Now link stuff
54 echo "Checking host files..."
55 for entry in $workdirs[@]; do
56 base=${entry:t}
57 dirn=${entry:h}
58 dirn=${dirn//$SPECBASE/}
59 dirn=${dirn//net:$DOMAIN/}
60 dirn=${dirn//host:$HOST/}
61 dirn=${dirn//sys:$OSNAME/}
62 dirn=${dirn//distri:$DISTRI/}
63 dirn=${dirn//user:$USER/}
64 dirn=${dirn//user:$SUDO_USER/}
65 dirn=${${(j:/:)dirn}//\/\/##/}
66 dirn=${${(j:/:)dirn}//\/##/}
67
68 echo "... ${HOME}/${dirn}/${base}"
69
70 linktarget=$(readlink -f "${HOME}/${dirn}/${base}")
71 if ! [[ $linktarget =~ .dotfiles ]] || [ -z "${linktarget}" ]; then
72 answer=N
73 echo "File exists. ${HOME}/${dirn}/${base} (is ${linktarget})"
74 vared -p 'Overwrite with symlink to ${entry}?: [y/N] ' answer
75 case ${answer} in
76 y|Y)
77 rm -f "${HOME}/${dirn}/${base}"
78 ;;
79 *)
80 echo "Not overwriting ${linktarget}"
81 continue
82 ;;
83 esac
84 fi
85
86 if [ -n "${dirn}" ]; then
87 mkdir -p "${HOME}/${dirn}"
88 if [ ! -L ${HOME}/${dirn}/${base} ]; then
89 echo "Linking ${entry} to ${HOME}/${dirn}/${base}"
90 ln -rs ${entry} ${HOME}/${dirn}
91 fi
92 else
93 if [ ! -L ${HOME}/${dirn}/${base} ]; then
94 echo "Linking ${entry} to ${HOME}/${dirn}"
95 ln -rs ${entry} ${HOME}/${base}
96 fi
97 fi
98 done
99
100 if [ -f "${CRONBASE}/${HOST}.${DOMAIN}" ]; then
101 if ! old=${${(z)"$(crontab -l | md5sum)"}[1]}; then
102 echo "crontab disabled for users"
103 return
104 fi
105 new=${${(z)$(md5sum "${CRONBASE}/${HOST}.${DOMAIN}" )}[1]}
106 if [ "${new}" != "${old}" ]; then
107 echo "Crontab changed, installing repository copy"
108 crontab "${CRONBASE}/${HOST}.${DOMAIN}"
109 fi
110 fi
111