#!/bin/sh
# Filename:      preinst
# Purpose:       preinst script for package grml-etc-core
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

set -e

case "$1" in
    install|upgrade|configure)
        TMP=$$
        for file in /etc/zsh/zlogin /etc/zsh/zprofile /etc/zsh/zshenv /etc/zsh/zshrc \
                    /etc/vim/vimrc /etc/tmux.conf; do
             # make sure we don't lose anything
             # yes this is ugly :(
             [ -e "$file".original ] || cp -f "$file" "${file}.tmp.$TMP" 2>/dev/null || true
             [ -e "$file" ] && cp -f "$file" "$file".grml-etc-core && \
             [ -f "$file" ] && rm "$file" && \
             dpkg-divert --quiet --add --rename --package grml-etc-core --divert "$file".original $file && \
             [ -f "$file".grml-etc-core ] && mv -f "$file".grml-etc-core "$file"
             [ -e "${file}.tmp.$TMP" ]    && mv -f "${file}.tmp.$TMP" "$file.original"
        done

        # we stopped shipping /etc/zsh/zlogout with v0.20.0,
        # make sure we don't leave it behind on updated systems
        if dpkg-divert --list grml-etc-core 2>/dev/null | grep -q '/etc/zsh/zlogout' ; then
            mv -f /etc/zsh/zlogout /etc/zsh/zlogout.old && \
            dpkg-divert --remove --rename --package grml-etc-core /etc/zsh/zlogout
        fi
    ;;
    *)
        echo "preinst called with unknown argument $1" >&2
        exit 1
esac

#DEBHELPER#

exit 0

## END OF FILE #################################################################
