#!/bin/sh
set -e
# A string that denotes the current version of F-Prot for Workstations:
version=4.3.4
IS_INSTALLED=false

# use debconf
. /usr/share/debconf/confmodule || exit
# db_capb backup
# FIXME: add backup capabilities to this script.

# If /usr/lib/f-prot/f-prot exists, we assume that F-Prot is already installed.
# This should be replaced by a more reliable method in the future, like e.g.
# checking a stored version string. But since earlier versions of f-prot-install
# didn't store the version and md5sum anywhere, we have to stick with this relatively
# unreliable method for the time being.

if [ -d /usr/lib/f-prot -a -f /usr/lib/f-prot/f-prot ]; then
    IS_INSTALLED=true
fi

# Maybe this is a dirty hack, but still the best way I could find:

db_get f-prot-installer/configured
if [ "$RET" = "true" ]; then
# we can safely exit here.    
    
    exit 0
fi


# If f-prot already exists we ask the user, what to do
# If noninteractive, we just leave it alone.

db_subst f-prot-installer/reinstall version $version
if [ $IS_INSTALLED = true ]; then
    db_fset f-prot-installer/reinstall seen false
    db_input high f-prot-installer/reinstall || true
    db_go
else
    
# With the f-prot binary not present, we
# can safely assume that F-Prot should be installed
    
    db_fset f-prot-installer/reinstall seen true
    db_set f-prot-installer/reinstall true
fi

db_get f-prot-installer/reinstall

# Ask this question only if f-prot is not yet installed or
# should be reinstalled

if [ "$RET" = "true" ]; then 
    
# Oh, well, we probably have to reset this, else F-Prot will never be
# updated on package upgrades (In the future we may use the stored
# version string instead.  Well, of course, the stored md5sum would be
# more reliable but I think we are not allowed to do any actual
# modifications at config time. Doing an unrequested download (i.e. of
# the md5sum) would probably fall into that category.)
#set -x    
#  The following message is obsolete now.
#    db_input medium f-prot-installer/renamed || true
    db_go
    db_fset f-prot-installer/action seen false
    db_input high f-prot-installer/action || true
    db_go
    db_get f-prot-installer/action
    case "$RET" in
	"Install from file")
	    db_fset f-prot-installer/where_are_files seen false
	    db_input critical f-prot-installer/where_are_files || true
	    db_go
	    db_fset f-prot-installer/update_defs seen false
	    db_input high f-prot-installer/update_defs
	    db_go
	    ;;
	"Download and install")
	    # If the program files are downloaded, it is safe to update the defs as well.
	    db_fset f-prot-installer/update_defs seen true
	    db_set f-prot-installer/update_defs true
	    db_go
	    ;;
	"Install later")
	    db_fset f-prot-installer/install_later seen false
	    db_input medium f-prot-installer/install_later
	    db_go
	    db_fset f-prot-installer/configured seen false
	    db_set f-prot-installer/configured false
	    db_go
	    exit 1
	    
	    ;;*)
	    
    esac

    
# O.K, here we are. If F-Prot is already installed or should be (re-)installed,
# ask whether to run the updater script after installation.
# Skip this question, if user doesn't wish to reinstall. In the latter case
# s/he probably doesn't wish to be bothered anymore.
    
    db_get f-prot-installer/action
    if [ ! "$RET" = "Install later" ]; then

#	db_input medium f-prot-installer/note_cron
#	db_go
	db_fset f-prot-installer/configured seen true
	db_go
	db_set f-prot-installer/configured true
	db_go
    fi
else

    # This 'else' is if f-prot-installer/reinstall was set to
    # "false". We need to set "f-prot-installer/configured" to "true"
    # to avoid a second run of the config script at the postinst
    # stage.

    db_fset f-prot-installer/configured seen true
    db_go
    db_set f-prot-installer/configured true
    db_go
fi

exit 0
