#! /bin/sh
#----------------------------------------------------------------------------
# deinstall-package - deinstall packages
#
# Copyright (c) 2001-2003   Michell Schimanski <m.schimanski@tu-bs.de>
#			    Frank Meyer <frank@eisfair.org>
#
# Creation:	09.12.2001  ms
# Last Update:  09.12.2001  ms
#               08.08.2002  Ansgar Püster (format of new info files)
#		19.07.2003  fm
#
# 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.
#----------------------------------------------------------------------------

echoyx () { echo -e "\033[$1;$2H\c"; shift; shift; echo -e "$*\c"; }

read_package_info ()
{
    file=$1

    package=''
    description=''
    version=''
    date=''
    author=''

    while read line
    do
	case "$line"
	in
	    "<package>"* | "<name>"*)
		set -- $line
		package="$2"
		[ "$package" = '' ] && package=`echo "$line" | sed 's#</*name>##g'`
		;;
	    "<description>"*)
		set -- $line
		shift
		[ "$description" = '' ] && description="$*"
		;;
	    "<short>"*)
		description=`echo "$line" | sed 's#</*short>##g'`
		;;
	    "<version>"*)
		set -- $line
		version="$2"
		[ "$version" = '' ] && version=`echo "$line" | sed 's#</*version>##g'`
		;;
	    "<date>"*)
		set -- $line
		date="$2"
		[ "$date" = '' ] && date=`echo "$line" | sed 's#</*date>##g'`
		;;
	    "<author>"*)
		set -- $line
		author="$*"
		author=`echo "$author" | sed 's#<author>##g' | sed 's#</author>##g'`
		;;
	    *)
		;;
	esac
    done < $file
}

normalize_version ()
{
    v=$1

    case $v
    in
	1.0beta)
	    v=0.90.0
	    ;;
	1.0beta-patch-*)
	    IFS='-'
	    set -- $v
	    unset IFS
	    v=0.90.$3
	    ;;
    esac

    case "$v"
    in
	[0-9]*.[0-9]*.[0-9]*)
	    IFS='.'
	    set -- $v
	    unset IFS
	    major=$1
	    minor=$2
	    revision=$3
	    ;;
	[0-9]*.[0-9]*)
	    IFS='.'
	    set -- $v
	    unset IFS
	    major=$1
	    minor=$2
	    revision=0
	    ;;
	*)
	    major=0
	    minor=0
	    revision=0
	    ;;
    esac
    version=$major.$minor.$revision
}

while [ 1 ]
do
    clrhome
    colecho "Remove packages" gn
    row=3

    n=0
    for j in /var/install/packages/*
    do
	n=`expr $n + 1`
	read_package_info $j
	normalize_version $version

	echoyx $row  5 "$n. $package"
	echoyx $row 40 "$version"
	echoyx $row 68 "$date"

	eval package_$n='"$j"'
	row=`expr $row + 1`

	if [ $row = 21 ]
	then
	    echo
	    echo
	    /var/install/bin/anykey
	    clrhome
	    colecho "Remove package" gn
	    row=3
	fi
    done

    echoyx 23 1 "Select package to remove (1-$n, ENTER=Return 0=Exit) "
    read a

    if [ "$a" = "0" ]
    then
	exit 127
    fi

    if [ "$a" = "" ]
    then
	exit 0
    fi

    eval file='$package_'$a
    read_package_info $file
    normalize_version $version

    clrhome
    colecho "Package information" gn
    echo

    echo "    Package:        $package"
    echo
    echo "    Description:    $description"
    echo
    echo "    Version:        $version"
    echo
    echo "    Date:           $date"
    echo
    echo "    Author:         $author"
    echo

    echoyx 23 1 "Remove package (y/n)? "
    read a

    if [ "$a" = "y" ]; then
	/var/install/bin/del-package $package
    fi
    /var/install/bin/anykey
done