update
authorJoerg Jaspert <joerg@debian.org>
Mon, 18 Mar 2013 21:51:19 +0000 (22:51 +0100)
committerJoerg Jaspert <joerg@debian.org>
Mon, 18 Mar 2013 21:51:19 +0000 (22:51 +0100)
.zsh/functions/udh

index e35935d..7ca7e04 100644 (file)
@@ -13,14 +13,17 @@ local SPECBASE=${HOME}/.dotfiles
 local CRONBASE=${HOME}/.crontabs
 local startdir=$(pwd)
 local STATEFILE=${HOME}/.config/mr/update
-
 local linktarget entry file base dirn opt force diff now age max
 
+trap "cd -q ${startdir}" ERR TERM HUP INT QUIT EXIT
+
+local chpwd_functions=""
+
 if [ "$ZDOTDIR" = "" ]; then
     echo "Sorry, need to run within a configured zsh environment"
     return
 fi
-cd -q ${HOME}
+cd ${HOME}
 
 while getopts "f" opt; do
   case $opt in
@@ -46,117 +49,120 @@ else
     (( max = 1 * 60 * 60 ))
     if [[ $diff -gt $max ]]; then
         mr update
+        force=1
     else
         echo "Too early retry"
-        return
     fi
 fi
-touch ${STATEFILE}
-
-# Now we check if we have something specific for this host, that we need
-# to link into the homedir
-
-typeset -a specdirs
-specdirs=(
-#    "${SPECBASE}/net:$DOMAIN"
-#    "${SPECBASE}/host:$HOST"
-#    "${SPECBASE}/sys:$OSNAME"
-#    "${SPECBASE}/distri:$DISTRI"
-#    "${SPECBASE}/user:$USER"
-#    "${SPECBASE}/user:$SUDO_USER"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST"
-    "${SPECBASE}/net:$DOMAIN/sys:$OSNAME"
-    "${SPECBASE}/net:$DOMAIN/distri:$DISTRI"
-    "${SPECBASE}/net:$DOMAIN/user:$USER"
-    "${SPECBASE}/net:$DOMAIN/user:$SUDO_USER"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/distri:$DISTRI"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$USER"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$SUDO_USER"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/distri:$DISTRI"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$USER"
-    "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
-    "${SPECBASE}/host:$HOST/sys:$OSNAME"
-    "${SPECBASE}/host:$HOST/distri:$DISTRI"
-    "${SPECBASE}/host:$HOST/user:$USER"
-    "${SPECBASE}/host:$HOST/user:$SUDO_USER"
-    "${SPECBASE}/host:$HOST/sys:$OSNAME/distri:$DISTRI"
-    "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$USER"
-    "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
-)
-
-typeset -a workdirs
-for entry in $specdirs[@]; do
-    for file in ${entry}/**/*(N.); do
-        workdirs+=$file
+
+if (( force )); then
+    touch ${STATEFILE}
+
+    # Now we check if we have something specific for this host, that we need
+    # to link into the homedir
+
+    typeset -a specdirs
+    specdirs=(
+    #    "${SPECBASE}/net:$DOMAIN"
+    #    "${SPECBASE}/host:$HOST"
+    #    "${SPECBASE}/sys:$OSNAME"
+    #    "${SPECBASE}/distri:$DISTRI"
+    #    "${SPECBASE}/user:$USER"
+    #    "${SPECBASE}/user:$SUDO_USER"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST"
+        "${SPECBASE}/net:$DOMAIN/sys:$OSNAME"
+        "${SPECBASE}/net:$DOMAIN/distri:$DISTRI"
+        "${SPECBASE}/net:$DOMAIN/user:$USER"
+        "${SPECBASE}/net:$DOMAIN/user:$SUDO_USER"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/distri:$DISTRI"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$USER"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/user:$SUDO_USER"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/distri:$DISTRI"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$USER"
+        "${SPECBASE}/net:$DOMAIN/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
+        "${SPECBASE}/host:$HOST/sys:$OSNAME"
+        "${SPECBASE}/host:$HOST/distri:$DISTRI"
+        "${SPECBASE}/host:$HOST/user:$USER"
+        "${SPECBASE}/host:$HOST/user:$SUDO_USER"
+        "${SPECBASE}/host:$HOST/sys:$OSNAME/distri:$DISTRI"
+        "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$USER"
+        "${SPECBASE}/host:$HOST/sys:$OSNAME/user:$SUDO_USER"
+    )
+
+    typeset -a workdirs
+    for entry in $specdirs[@]; do
+        for file in ${entry}/**/*(N.); do
+            workdirs+=$file
+        done
     done
-done
-typeset -U workdirs
-
-# Now link stuff
-echo "Checking host files..."
-for entry in $workdirs[@]; do
-    base=${entry:t}
-    dirn=${entry:h}
-    dirn=${dirn//$SPECBASE/}
-    dirn=${dirn//net:$DOMAIN/}
-    dirn=${dirn//host:$HOST/}
-    dirn=${dirn//sys:$OSNAME/}
-    dirn=${dirn//distri:$DISTRI/}
-    dirn=${dirn//user:$USER/}
-    dirn=${dirn//user:$SUDO_USER/}
-
-    echo "... ${HOME}/${dirn}/${base}"
-
-    if [ -f "${HOME}/${dirn}/${base}" ] || [ -L "${HOME}/${dirn}/${base}" ]; then
-        linktarget=$(readlink -f "${HOME}/${dirn}/${base}")
-        if ! [[ $linktarget =~ .dotfiles ]] || [ -z "${linktarget}" ]; then
-            answer=N
-            echo "File exists. ${HOME}/${dirn}/${base} (is ${linktarget})"
-            vared -p 'Overwrite with symlink to ${entry}?: [y/N] ' answer
-            case ${answer} in
-                y|Y)
-                    rm -f "${HOME}/${dirn}/${base}"
-                    ;;
-                *)
-                    echo "Not overwriting ${linktarget}"
-                    continue
-                    ;;
-            esac
+    typeset -U workdirs
+
+    # Now link stuff
+    echo "Checking host files..."
+    for entry in $workdirs[@]; do
+        base=${entry:t}
+        dirn=${entry:h}
+        dirn=${dirn//$SPECBASE/}
+        dirn=${dirn//net:$DOMAIN/}
+        dirn=${dirn//host:$HOST/}
+        dirn=${dirn//sys:$OSNAME/}
+        dirn=${dirn//distri:$DISTRI/}
+        dirn=${dirn//user:$USER/}
+        dirn=${dirn//user:$SUDO_USER/}
+
+        echo "... ${HOME}/${dirn}/${base}"
+
+        if [ -f "${HOME}/${dirn}/${base}" ] || [ -L "${HOME}/${dirn}/${base}" ]; then
+            linktarget=$(readlink -f "${HOME}/${dirn}/${base}")
+            if ! [[ $linktarget =~ .dotfiles ]] || [ -z "${linktarget}" ]; then
+                answer=N
+                echo "File exists. ${HOME}/${dirn}/${base} (is ${linktarget})"
+                vared -p 'Overwrite with symlink to ${entry}?: [y/N] ' answer
+                case ${answer} in
+                    y|Y)
+                        rm -f "${HOME}/${dirn}/${base}"
+                        ;;
+                    *)
+                        echo "Not overwriting ${linktarget}"
+                        continue
+                        ;;
+                esac
+            fi
         fi
-    fi
 
-    linktarget=$(relative "${entry}" "${HOME}/${dirn}")
-    # The same can be done with ln - current sid or later versions.
-    # (as of 2013-16-03)
-#    if ln --help 2>/dev/null | grep -- --relative >/dev/null; then
-#        LNOPTS="-r"
-#    fi
-    if [[ -n ${dirn} ]]; then
-        mkdir -p "${HOME}/${dirn}"
-        if [[ ! -L ${HOME}/${dirn}/${base} ]]; then
-            echo "Linking ${entry} to ${HOME}/${dirn}/${base}"
-            /bin/ln -s "${linktarget}" "${HOME}/${dirn}"
-        fi
-    else
-        if [[ ! -L ${HOME}/${dirn}/${base} ]]; then
-            echo "Linking ${entry} to ${HOME}/${dirn}"
-            /bin/ln -s "${linktarget}" "${HOME}/${base}"
+        linktarget=$(relative "${entry}" "${HOME}/${dirn}")
+        # The same can be done with ln - current sid or later versions.
+        # (as of 2013-16-03)
+    #    if ln --help 2>/dev/null | grep -- --relative >/dev/null; then
+    #        LNOPTS="-r"
+    #    fi
+        if [[ -n ${dirn} ]]; then
+            mkdir -p "${HOME}/${dirn}"
+            if [[ ! -L ${HOME}/${dirn}/${base} ]]; then
+                #echo "Linking ${entry} to ${HOME}/${dirn}/${base}"
+                echo "Linking ${linktarget} to ${HOME}/${dirn}"
+                /bin/ln -s "${linktarget}" "${HOME}/${dirn}"
+            fi
+        else
+            if [[ ! -L ${HOME}/${dirn}/${base} ]]; then
+#                echo "Linking ${entry} to ${HOME}/${dirn}"
+                echo "Linking ${linktarget} to ${HOME}/${base}"
+                /bin/ln -s "${linktarget}" "${HOME}/${base}"
+            fi
         fi
-    fi
-done
+    done
 
-if [ -f "${CRONBASE}/${HOST}.${DOMAIN}" ]; then
-    if ! old=${${(z)"$(crontab -l | md5sum)"}[1]}; then
-        echo "crontab disabled for users"
-        return
-    fi
-    new=${${(z)$(md5sum "${CRONBASE}/${HOST}.${DOMAIN}" )}[1]}
-    if [ "${new}" != "${old}" ]; then
-        echo "Crontab changed, installing repository copy"
-        crontab "${CRONBASE}/${HOST}.${DOMAIN}"
+    if [ -f "${CRONBASE}/${HOST}.${DOMAIN}" ]; then
+        if ! old=${${(z)"$(crontab -l | md5sum)"}[1]}; then
+            echo "crontab disabled for users"
+            return
+        fi
+        new=${${(z)$(md5sum "${CRONBASE}/${HOST}.${DOMAIN}" )}[1]}
+        if [ "${new}" != "${old}" ]; then
+            echo "Crontab changed, installing repository copy"
+            crontab "${CRONBASE}/${HOST}.${DOMAIN}"
+        fi
     fi
 fi
-
-cd -q ${startdir}