#!/bin/sh
# Tar installer object
#
# This file is saved on the disk when the .tar package is installed by
# vmware-install.pl. It can be invoked by any installer.

#
# Global variables
#

vmware_db='/etc/vmware/locations'
vmware_db_tmp='/etc/vmware/locations.tmp'

#
# Tools
#

# Load an answer from the database
db_load_answer() {
    id="$1"

    grep '^remove_answer '"$id"'$\|^answer '"$id"' ' "$vmware_db" | tail -1 | sed -n 's/^answer '"$id"' //p'
}

# Load the database in the db_* variables
db_load() {
    db_answers=''
    if [ -r "$vmware_db" ]; then
        for i in `sed -n -e 's/^answer \(.*\) .*/\1/p' "$vmware_db"`; do
            value=`db_load_answer "$i"`
            if [ "$value" != '' ]; then
                eval 'db_answer_'"$i"'="$value"'
                db_answers="$db_answers"' '"$i"' '
            fi
        done
    fi
}

#
# Implementation of the methods
#

# Return the human-readable name of the installer
installer_kind() {
    echo 'tar'

    exit 0
}

# Convert the installer database format to formats used by older installers
# The output should be a .tar.gz containing enough information to allow a
# clean "upgrade" (which will actually be a downgrade) by an older installer
installer_convertdb() {
    version="$1"
    output="$2"

    case "$version" in
        tar)
            echo 'Keeping the tar installer database format.'
	    echo
            # Same format. Nothing to do.
            tar -czopf "$output" "$vmware_db" 2> /dev/null

            exit 0
	    ;;

        rpm)
            echo 'Converting the tar installer database format'
            echo '        to the rpm installer database format.'
	    echo
            # Same format. Nothing to do.
            tar -czopf "$output" "$vmware_db" 2> /dev/null

            exit 0
	    ;;

        old)
            echo 'Converting the tar installer database format'
            echo '        to the old installer database format.'
	    echo
            db_load
            # Backup the existing db
            cp "$vmware_db" "$vmware_db_tmp"
            # Write the converted db
            rm -f "$vmware_db"
            for i in $db_answers; do
                eval 'value="$db_answer_'"$i"'"'
                if [ "$i" = 'BUILDR_vmmon' ]; then
                    i='BUILDR_driver'
                fi
                echo 'answer '"$i"' '"$value" >> "$vmware_db"
            done
            # Make the old uninstaller look good
            echo 'VERSION 1.1.2' >> "$vmware_db"
            # Now pack the converted db
            tar -czopf "$output" "$vmware_db" 2> /dev/null
            # And restore the current db
            mv "$vmware_db_tmp" "$vmware_db"

            exit 0
	    ;;

        *)
	    echo 'Unknown '"$version"' installer database format.'
	    echo

            exit 1
	    ;;
    esac
}

# Uninstall what has been installed by the installer.
# This should never prompt the user, because it can be annoying if invoked
# from the rpm installer for example.
installer_uninstall() {
    echo 'Uninstalling the tar installation of VMware.'
    echo

    bindir=`db_load_answer 'BINDIR'`
    if [ "$bindir" = '' ]; then
        echo 'Error: Unable to find the binary installation directory (answer BINDIR)'
        echo '       in '"$vmware_db"'.'
	echo

        exit 1
    fi

    # Remove the package
    if [ ! -x "$bindir"/vmware-uninstall.pl ]; then
        exit 1
    fi
    "$bindir"/vmware-uninstall.pl || exit 1
    

    exit 0
}

#
# Interface of the methods
#

case "$1" in
    kind)
	installer_kind
	;;

    convertdb)
	installer_convertdb "$2" "$3"
	;;

    uninstall)
	installer_uninstall
	;;

    *)
	echo 'Usage: '`basename "$0"`' {kind|convertdb|uninstall}'
	echo

	exit 1
	;;
esac

