+++ /dev/null
-# -*- mode:sh -*-
-
-emulate -LR zsh
-setopt extendedglob
-setopt globdots
-setopt numericglobsort
-
-autoload -Uz relative
-zmodload zsh/stat
-
-
-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 ${HOME}
-
-while getopts "f" opt; do
- case $opt in
- (f)
- force=1
- ;;
- (*)
- return 1
- ;;
- esac
-done
-shift $(( OPTIND - 1 ))
-
-if (( force )); then
- mr update
-else
- if [[ ! -f ${STATEFILE} ]]; then
- touch ${STATEFILE}
- fi
- age=$(zstat +ctime ${STATEFILE})
- now=$(date +%s)
- (( diff = now - age ))
- (( max = 1 * 60 * 60 ))
- if [[ $diff -gt $max ]]; then
- mr update
- force=1
- else
- echo "Too early retry"
- fi
-fi
-
-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
- 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
-
- 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
- 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}"
- fi
- fi
-
- if [[ ! -f ${ZDOTDIR}/zshenv.local ]] || [[ ! -L ${ZDOTDIR}/zshenv.local ]]; then
- echo "Linking zshenv.local.sample to zshenv.local"
- cd ${ZDOTDIR}
- ln -s zshenv.local.sample zshenv.local
- fi
-
-fi