#!/bin/sh
# ----------------------------------------------------------------------------
# /var/install/deinstall/climm - deinstall script
#
# Creation   : 2008-12-13 alex
# Last update: $Id: climm 198 2008-12-13 14:37:26Z alex $
#
# Copyright (c) 2008 the eisfair team, <team(at)eisfair(dot)org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# ----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

# Set package name
packageName=climm

# Set filelist
filelist=/etc/filelist.d/climm-files.txt

# Check if this is an update
if [ "${1}" == "update" ]
then
    mecho -info "Updating package 'climm' (removing previous version)"
    update=true
    
    # ----------------------------------------------------------------------
    # Add everything to this list what should _not_ be removed during an 
    # update. This can be folders too, but let the bash expand their content
    # using an '*' like 'foo/bar/*'. The entries listet here must be given
    # on the file list of the package. All other stuff must be remove by hand
    # using an additional line like 'rm /foo/bar/foobar.txt'.
    filesToLeave="etc/config.d/climm
                  etc/backup.d/climm*
                  etc/filelist.d/climm-files.txt
                  var/install/deinstall/climm
                  var/install/menu/setup.services.climm*"
else
    mecho -info "Removing package 'climm'"
    update=false
    filesToLeave=''
fi

# ----------------------------------------------------------------------------
# Remove package content except some special files if it's an update
# instead of a deinstallation
# ----------------------------------------------------------------------------
while read id perm user group package file
do
    # ------------------------------------------------
    # Remove file only if it is not on the ignore list
    removeCurrentFile=true
    for currentFileToLeave in "${filesToLeave}"
    do
        if [ "${currentFileToLeave}" == "${file}" ]
        then
            removeCurrentFile=false
            break
        fi
    done
    
    if ${removeCurrentFile}
    then
        case ${id} in
            b|u)
                if [ "${file}" != "tmp/install.sh" -a "${file}" != "tmp/preinstall.sh" ]
                then
                    rm -f /${file}
                fi
            ;;
    
            f)
                # Remove directories
                rmdir --ignore-fail-on-non-empty /${file}
            ;;
        esac
    fi
done < ${filelist}



# ----------------------------------------------------------------------------
# Check files and default config must be removed separately because they
# are not on the file list if the package is used as it was created by
# create-package-new.sh, they are created dynamically during installation.
# ----------------------------------------------------------------------------
rm -f /etc/check.d/climm*
rm -f /etc/default.d/climm*



# ----------------------------------------------------------------------------
# Stop remove for update only
# ----------------------------------------------------------------------------
if [ "$update" == true ]
then
    exit 0
fi



# ----------------------------------------------------------------------------
# Remove package from menu system / remove all menu and config files
# ----------------------------------------------------------------------------
/var/install/bin/del-menu setup.services.menu setup.services.climm.menu

# ----------------------------------------------------------------------------
# Remove config file
# ----------------------------------------------------------------------------
rm -f /etc/config.d/climm
rm -f /etc/backup.d/climm*
rm -f /etc/filelist.d/climm-files.txt

# ----------------------------------------------------------------------------
# Remove deinstall script
# ----------------------------------------------------------------------------
rm -f /var/install/deinstall/climm

# ----------------------------------------------------------------------------
exit 0
