#!/bin/sh
# @vasm : VLAPT
# @level: root
# Package management using slapt-get engine
# 
# (c) Eko M. Budi, 2003
# (c) VLocity Linux, 2003
#
# Released under GNU GPL


if [ -z "$DISPLAY" ]; then
cat << EOF
vlapt has been deprecated, in the favor of gslapt,
a nice GUI interface for slapt-get.
EOF
exit 0
fi

VDIR=$(dirname $0)
export PATH="$VDIR:$PATH"

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
cat << EOF
vlapt - VLocity Linux package management system
        using slapt-get backend
usage : vlapt

For more information, please read 'man slapt-get'
See /etc/slapt-get/slaptgetrc for the global configuration
EOF
   exit 0
fi

if [ ! $UID = 0 ]; then
    # exec $VDIR/vsuper "Super-VLAPT" "$0 --super-vlapt"
    exec $VDIR/vsuper "Super-VLAPT" gslapt
fi

. $VDIR/vasm-functions

# Normal service begin here
SLAPT_RC="/etc/slapt-get/slapt-getrc"
VLAPT_RC="/etc/slapt-get/vlapt-getrc"
SEARCH=""

if [ -f /etc/vlocity-version ]; then
    RELEASE=`cat /etc/vlocity-version`
    VERSION=${RELEASE##* }
    VERSION=${VERSION%_*}
    VERSION=${VERSION%-*}
    VERSION=${VERSION%rc}
fi
VERSION=${VERSION:-5.0}

##################################################################
# Utilities
# count the selections
count_list()
{
    NUM=0
    while [ "$1" ]; do
	NUM=`expr $NUM '+' 1`
	shift
    done
    echo $NUM
}

echo_gauge() {
    if [ "$CMD" ]; then
	echo $1
	echo "XXX"
	echo $2
	echo "XXX"
    else
	echo "XXX"
	echo $1
	echo "\n$2"
	echo "XXX"
    fi
}


menu_list()
{
DEF_ITEM="USAGE"

TITLE="LIST PACKAGES"
TEXT="\n
How do you want to list the available packages ?"
DIMENSION="40 16 4"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--default-item $DEF_ITEM --menu "$TEXT" $DIMENSION \
"USAGE"   "List packages based on usage" \
"LIBRARY"  "List packages based on base libraries" \
"SEARCH"   "Specify search expression" \
"ALL"   "All packages" \
2> $freply

   status=$?
   [ $status != 0 ] && return $status

    case `cat $freply` in
      USAGE)
        menu_usage
	DEF_ITEM="USAGE"
	;;
      UPGRADE)
        menu_library
	DEF_ITEM="LIBRARY"
	;;
      SEARCH)
        menu_search
	DEF_ITEM="SEARCH"
	;;
      ALL)
        SEARCH=""
	return 0
	;;
    esac
return 0

}




##################################################################
# UPDATE, get info from sources
do_update()
{
    echo "Updating packages info, please wait until finished" > $freply
    echo "If it takes too long, you should check the SOURCEs" >> $freply
    ( slapt-get -y --update 2>&1 | sed 's/\.\..*Done/\.\.\.Done/' | \
	sed 's/\.\..*Cached/\.\.\.Cached/' >> $freply
      echo "--------------------------------" >> $freply
      echo "UPDATE INFO HAS FINISHED " >> $freply
      echo "Press [Enter] to continue" >> $freply ) &
    $DCMD --backtitle "$BACKTITLE" --title "UPDATE" --tailbox $freply 20 74

    return 0
}

build_update_menu()
{
    RET=1
    while read LINE; do
	MENU=`echo $LINE | cut -f2 -d=`
        echo "'$MENU' '' \\" >> $fmenu
	RET=0
    done
    return $RET
}

menu_update()
{
TITLE="UPDATE PACKAGES INFO"
TEXT="\n
Update gets the latest packages+dependency info from the SOURCE sites.
You need to do this if you changed the SOURCEs using the CONFIG menu,
or you think the sources have been upgraded since the last update.\n
We are about to update the info from the following sources.
Press <OK> to continue:"
DIMENSION="20 76 6"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
    if ! grep -e "^SOURCE=.*" $SLAPT_RC | build_update_menu; then
	msgbox "Please set the SOURCE first using the CONFIG menu" "ERROR"
	return 0
    fi
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    do_update
    return 0
}

##################################################################
# UPGRADE
do_upgrade()
{
    ( 
    echo "Upgrading packages, this may take time ..." > $freply
    echo "Please wait until told it is finished" > $freply
    for PKG in $PKG_LIST; do
	slapt-get -y --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $freply
    echo "UPGRADE PACKAGE HAS FINISHED " >> $freply
    echo "Press [Enter] to continue" >> $freply
    ) &
    $DCMD --backtitle "$BACKTITLE" --title "INSTALL PACKAGE" \
    --tailbox $freply 20 74 2> /dev/null
    return 0
}

build_upgrade_menu()
{
    RET=1
    while read LINE; do
	PKG1=`echo $LINE | cut -f1 -d ' '`
	VER2=`echo $LINE | cut -f8 -d ' '`
	PNAME=`pkgname $PKG1`
        echo "'$PNAME-$VER2' 'over $PKG1' on \\" >> $fmenu
	RET=0
    done
    return $RET
}

menu_upgrade()
{
TITLE="UPGRADE PACKAGES"
TEXT="\n
This menu allows you to upgrade the installed packages with newer 
version from the sources. Please select the ones to upgrade:"
DIMENSION="20 76 11"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! slapt-get -s --upgrade | grep "is to be upgraded" | build_upgrade_menu; then
	msgbox "Congratulation, your system is up to date" "NO NEWER PACKAGE"
	return 0
    fi
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_LIST=`cat $freply`
    do_upgrade
    return 0
}


##################################################################
# INSTALL
do_install()
{
    ( 
    echo "Installing packages, please wait until finished" > $freply
    for PKG in $PKG_LIST; do
	slapt-get -y --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $freply
    echo "INSTALL PACKAGE HAS FINISHED " >> $freply
    echo "Press [Enter] to continue" >> $freply
    ) &
    $DCMD --backtitle "$BACKTITLE" --title "INSTALL PACKAGE" \
    --tailbox $freply 20 74 2> /dev/null
    return 0
}

build_install2_menu()
{
    if ! slapt-get -s --install $PKG_CHOICE 2>/dev/null | grep "is to be installed" >$freply; then
	return 1
    fi
    cat $freply | while read LINE; do
	PLONG=`echo $LINE | cut -f1 -d ' '`
	PNAME=`pkgname $PLONG`
	PVER=`slapt-get --show $PNAME | grep -e "^Package Version" | cut -f2 -d:`
	PVER=`echo $PVER`
	DESC=`slapt-get --show $PLONG | grep -m 1 -e "^$PNAME:" | cut -f2 -d:`
	if echo $DESC | grep -qe "(.*)"; then
	    DESC=`echo $DESC | cut -f2 -d '(' | cut -f1 -d')'`
	else
	    DESC=`echo $DESC`
	fi
        echo "'$PNAME-$PVER' '$DESC (required)' on \\" >> $fmenu
    done
    SUGGESTS_LIST=`echo $SUGGESTS_LIST | sed 's!,! !g'`
    for PNAME in $SUGGESTS_LIST; do
	PVER=`slapt-get --show $PNAME | grep -e "^Package Version" | cut -f2 -d:`
	[ -z "$PVER" ] && continue
	PVER=`echo $PVER`
	DESC=`slapt-get --show $PNAME | grep -m 1 -e "^$PNAME:" | cut -f2 -d:`
	if echo $DESC | grep -qe "(.*)"; then
	    DESC=`echo $DESC | cut -f2 -d '(' | cut -f1 -d')'`" "
	else
	    DESC=`echo $DESC`" "
	fi
        echo "'$PNAME-$PVER' '$DESC(suggested)' off \\" >> $fmenu
    done
    return 0
}

menu_install2()
{
    SUGGESTS_LIST=`slapt-get -show $PKG_CHOICE | grep "Package Suggests:" | cut -f2 -d:`
    SIZE1=`slapt-get -s --install $PKG_CHOICE 2> $fmenu | grep -e "^Need to get"`
    if [ $? != 0 ]; then
	$DCMD --backtitle "$BACKTITLE" --title "ERROR" \
	    --textbox $fmenu 12 60 2> /dev/null
	return 255
    fi
    PKGNAME=`pkgname $PKG_CHOICE`
    DESCRIPTION=`slapt-get -show $PKG_CHOICE | egrep "^$PKGNAME:" | \
    cut -f2- -d: | egrep -m 7 -iv '(^ *$|website:|author:|authors:|license:)' `
TEXT="\n
About to install package $PKG_CHOICE and friends.\n
$DESCRIPTION\n
$SIZE1 Please confirm then hit <OK>:"
TITLE="INSTALL PACKAGES"
DIMENSION="20 76 6"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_install2_menu; then
	msgbox "No package to be installed" "SORRY"
	return 255
    fi
    echo '2> $freply' >> $fmenu
    
    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_LIST=`cat $freply`
    do_install
    return 0
}

build_install1_menu()
{
    if [ "$SEARCH" ]; then
	slapt-get --search "$SEARCH" | grep "inst=no" > $freply
    else
	slapt-get --list | grep "inst=no" > $freply
    fi
    if [ "`head -n 1 $freply`" ]; then
	cat $freply | while read LINE; do
	    PNAME=`echo $LINE | cut -f1 -d ' '`
	    PVER=`echo $LINE | cut -f2 -d ' '`
	    PDESC=`echo $LINE | cut -f2 -d: | sed "s/'//g"`
	    if echo $PDESC | grep -qe "(.*)"; then
		PDESC=`echo $PDESC | cut -f2 -d '(' | cut -f1 -d')'`
	    else
		PDESC=`echo $PDESC`
	    fi
    	    echo "'$PNAME-$PVER' '$PDESC' \\" >> $fmenu
	done
	return 0
    fi
    return 1
}

menu_install1()
{
while [ 0 ]; do
TITLE="INSTALL PACKAGE"
TEXT="\n
Please select a package to be installed. A confirmation dialog will
let you know all of the required packages before actually install it:"
DIMENSION="20 76 11"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_install1_menu; then
	msgbox "Search found no package" "SORRY"
	return 255
    fi 
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_CHOICE=`cat $freply`
    menu_install2

    [ $? = 1 ] && return 1

done
}

menu_install()
{
while [ 0 ]; do
TITLE="INSTALL PACKAGE"
TEXT="\n
This wizard helps you to install a package and its dependencies.\n
It will present you the list of available packages. You may narrow
the list by specifying search keyword, for example:\n
package name (kernel,MPlayer,office,firefox,games,...)\n
group (/DEV,/EDIT,/EDU,/ENT,/GR,/MM,/NET,/OFC,/SET,/SYS,/UTIL,/CORE,/BASE)\n
library (/cli,/x11,/gtk,/gtk2,/gnome,/qt,/kde,/sdl,/java)\n\n

Just let this empty if you want to know all available packages:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
      --inputbox "$TEXT" 18 76 "$SEARCH" 2> $freply || return $?

SEARCH=`cat $freply`
SEARCH=`echo $SEARCH | sed 's/ /|/g'`

menu_install1

[ $? = 1 ] && return 0

done

}

##################################################################
# INSTALL NODEP
do_nodep()
{
    ( 
    for PKG in $PKG_CHOICES; do
        echo "Installing package $PKG, please wait ..." > $freply
	slapt-get -y --no-dep --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $freply
    echo "INSTALL PACKAGE HAS FINISHED" >> $freply
    echo "Press [Enter] to continue" >> $freply
    ) &
    $DCMD --backtitle "$BACKTITLE" --title "INSTALL PACKAGE" \
    --tailbox $freply 20 74 2> /dev/null
    return 0
}

build_nodep1_menu()
{
    if [ "$SEARCH" ]; then
	slapt-get --search "$SEARCH" > $freply
    else
	slapt-get --list > $freply
    fi
    if [ "'grep -c $freply'" != "0" ]; then
	cat $freply | grep "inst=no" | while read LINE; do
	    PNAME=`echo $LINE | cut -f1 -d ' '`
	    PVER=`echo $LINE | cut -f2 -d ' '`
	    PDESC=`echo $LINE | cut -f2 -d: | sed "s/'//g"`
	    if echo $PDESC | grep -q "("; then
		PDESC=`echo $PDESC | cut -f2 -d '(' | cut -f1 -d')'`
	    else
		PDESC=`echo $PDESC`
	    fi
    	    echo "'$PNAME-$PVER' '$PDESC' off \\" >> $fmenu
	done
	return 0
    fi
    return 1
}

menu_nodep1()
{
while [ 0 ]; do
TITLE="INSTALL PACKAGES NODEP"
TEXT="\n
Please select some packages to be installed. No dependencies checking
will be performed, so it is up to you:"
DIMENSION="20 76 11"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_nodep1_menu; then
	msgbox "No package was found" "SORRY"
	return 255
    fi 
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_CHOICES=`cat $freply`
    do_nodep

    [ $? = 1 ] && return 1
done
}

menu_nodep()
{
while [ 0 ]; do
TITLE="INSTALL PACKAGES NODEP"
TEXT="\n
This menu allows installing several packages without dependency checking.
Please enter the keyword to search the available packages, for example:\n
package name (kernel,MPlayer,office,firefox,games,...)\n
group (/DEV,/EDIT,/EDU,/ENT,/GR,/MM,/NET,/OFC,/SET,/SYS,/UTIL,/CORE,/BASE)\n
library (/cli,/x11,/gtk,/gtk2,/gnome,/qt,/kde,/sdl,/java)\n\n
Empty keyword will list all available packages:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
      --inputbox "$TEXT" 20 76 "$SEARCH" 2> $freply || return $?

SEARCH=`cat $freply`
SEARCH=`echo $SEARCH | sed 's/ /|/g'`

menu_nodep1

[ $? = 1 ] && return 0

done

}

##################################################################
#
do_remove()
{
    (
    echo "Removing packages, please wait ..." > $freply
    for CHOICE in $PKG_CHOICES; do
	removepkg $CHOICE 2>&1 >> $freply 
    done
    echo "--------------------------------" >> $freply
    echo "REMOVE PACKAGE HAS FINISNED" >> $freply
    echo "Press [Enter] to continue" >> $freply 
    ) &
    $DCMD --backtitle "$BACKTITLE" --title "REMOVE PACKAGE" \
    --tailbox $freply 20 74 2>/dev/null
}

build_remove1_menu()
{
for CHOICE in $PKG_CHOICES; do
    slapt-get -s --remove $CHOICE | grep "is to be removed" | while read LINE; do
	PLONG=`echo $LINE | cut -f1 -d ' '`
	PNAME=`pkgname $PLONG`
	if ! grep -q "^'$PLONG'" $fmenu; then
	    DESC=`slapt-get --show $PLONG | grep -m 1 -e "^$PNAME:" | cut -f2 -d:`
	    if echo $DESC | grep -q "("; then
		DESC=`echo $DESC | cut -f2 -d '(' | cut -f1 -d')'`
	    else
		DESC=`echo $DESC`
	    fi
    	    echo "'$PLONG' '$DESC' on \\" >> $fmenu
	fi
    done
done
}

menu_remove1()
{
SIZE=`slapt-get -s --remove $PKG_CHOICE | grep -e "^After unpacking"`
TEXT1="\n
About to remove the selected packages and the dependants as follows.
In respect to your judgement, you may cancel individual packages,
although it is not recomended.\n
Press <OK> if you are really really sure !!!"

TITLE1="REMOVE PACKAGES"
DIMENSION1="20 76 9"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE1" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT1" $DIMENSION1 \' >> $fmenu
    build_remove1_menu
    echo "2> $freply" >> $fmenu
    
    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_CHOICES=`cat $freply`
    
    if [ "$PKG_CHOICES" ]; then
	do_remove
	return 0
    fi
    return 1
}

build_remove_menu()
{
    ls -1 /var/log/packages > $freply
    PKG_NUM=`grep -c "" $freply`
    [ "$PKG_NUM" = "0" ] && return 1
    (
    PROGRESS=0
    cat $freply | while read LINE; do
	PNAME=`pkgname $LINE`
	DESC=`cat /var/log/packages/$LINE 2>/dev/null | grep -m 1 -e "^$PNAME:" | sed "s/'//"`
	if echo "$DESC" | grep -q "("; then
	    DESC=`echo $DESC | cut -f2 -d '(' | cut -f1 -d')'`
	else
	    DESC=${DESC#$PNAME: }
	fi
        echo "'$PNAME' '$DESC' off \\" >> $fmenu
	PROGRESS=`expr $PROGRESS '+' 1`
	pct=`expr 100 '*' $PROGRESS '/' $PKG_NUM`
	msg="Package $PROGRESS: $LINE"
	echo_gauge $pct "$msg"
    done
    ) | $DCMD --backtitle "$BACKTITLE" --title "COLLECTING INFO ..." \
	--gauge "\nPlease wait" 8 66 0
}

menu_remove()
{
while [ 0 ]; do
TITLE="REMOVE PACKAGES"
TEXT="\n
In this list you may select several packages to be removed, along with their
dependants. A confirmation dialog will appear before the actual removal:"
DIMENSION="20 76 11"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_remove_menu; then
	msgbox "No package is installed, very strange" "SORRY"
	return 1  
    fi
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_CHOICES=`cat $freply`
    menu_remove1 && return 0

done

}

##################################################################
# MIRROR
do_mirror()
{
    PKG_NUM=`count_list $PKG_LIST`
    (
    PROGRESS=0
    for PKG in $PKG_LIST; do
	PROGRESS=`expr $PROGRESS '+' 1`
	pct=`expr 90 '*' $PROGRESS '/' $PKG_NUM`
	msg="Download $PKG ($PROGRESS of $PKG_NUM)"
	echo_gauge $pct "$msg"
	sleep 2
	slapt-get --no-dep --download-only --install $PKG &>/dev/null
    done
    echo_gauge 95 "Setup dependency info"
    (cd $WORKDIR; makeslapt all &> /dev/null)
    echo_gauge 100 "DONE"
    sleep 3
    ) | $DCMD --backtitle "$BACKTITLE" --title "MIRRORING ..." --gauge "" 8 66 0
}

build_mirror_menu()
{
    slapt-get --search '\-vl[0-9]+' > $freply
    if [ "'grep -c $freply'" != "0" ]; then
	cat $freply | while read LINE; do
	    NAME=`echo $LINE | cut -f1 -d ' '`
	    VER=`echo $LINE | cut -f2 -d ' '`
	    DESC=`echo $LINE | cut -f2- -d: | sed "s/'//g"`
	    if echo $DESC | grep -q "("; then
		DESC=`echo $DESC | cut -f2 -d '(' | cut -f1 -d')'`
	    else
		DESC=`echo $DESC`
	    fi
    	    echo "'$NAME-$VER' '$DESC' on \\" >> $fmenu
	done
	return 0
    fi
    return 1
}

menu_mirror()
{
WORKDIR=`grep "^WORKINGDIR=" $SLAPT_RC | cut -f2 -d=`
WORKDIR=${WORKDIR:-/home/ftp/pub/veclinux-$VERSION}
mkdir -p $WORKDIR

TITLE="SETUP MIRROR"
TEXT="This menu downloads the packages into WORKDIR and setup dependency info,
so you can use it as a mirror by using an FTP/HTTP server.\n
Current WORKDIR=$WORKDIR.\n
Please select the packages you want to download:"
DIMENSION="20 76 10"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_mirror_menu; then
	msgbox "No VLocity Linux packages are available" "SORRY"
	return 255
    fi 
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_LIST=`cat $freply`
    do_mirror
    return 0
}




##################################################################
# SOURCE
do_source()
{
SGROUP=$1
CGROUP=""
rm -f $SLAPT_RC.new
    cat $SLAPT_RC | while read LINE; do
	if [ "$SGROUP" = "$CGROUP" ] && echo "$LINE" | grep -q "SOURCE="; then
	    SITE=`echo $LINE | cut -f2 -d=`
	    if echo "$SOURCE_LIST " | grep -wq "$SITE"; then
		echo "SOURCE=$SITE" >> $SLAPT_RC.new
	    else
		echo "#SOURCE=$SITE" >> $SLAPT_RC.new
	    fi
	else
	    echo "$LINE" >> $SLAPT_RC.new
        fi
	if echo $LINE | grep -qe "#SOURCE_GROUP=.*"; then
	    CGROUP=`echo $LINE | cut -f2 -d=`
	fi
    done
    mv -f $SLAPT_RC $SLAPT_RC.bak
    mv -f $SLAPT_RC.new $SLAPT_RC
}

build_source_menu()
{
    SGROUP=$1
    CGROUP=""
    RETVAL=1
    while read LINE ; do
	if [ "$SGROUP" = "$CGROUP" ]; then
	    if echo $LINE | grep -qe "SOURCE=.*"; then
		SRC=`echo $LINE | cut -f1 -d=`
		SITE=`echo $LINE | cut -f2 -d=`
		if [ "$SRC" = "SOURCE" ]; then
    		    echo "'$SITE' '' on \\" >> $fmenu
		else
    		    echo "'$SITE' '' off \\" >> $fmenu
		fi
		RETVAL=0
	    fi
	fi
	if echo $LINE | grep -qe "#SOURCE_GROUP=.*"; then
	    if [ "$SGROUP" = "$CGROUP" ]; then
		break
	    else
		CGROUP=`echo $LINE | cut -f2 -d=`
	    fi
	    # echo $LINE $CGROUP $SGROUP
	fi
    done
    return $RETVAL
}

menu_source()
{
SGROUP=$1
TITLE="SOURCES"
TEXT="\n
Please select local directory, FTP site, or HTTP site for sources.\n
You should choose the nearest mirror site."

DIMENSION="20 76 12"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    if ! cat $SLAPT_RC | build_source_menu $SGROUP; then
	msgbox "Cannot find $GROUP source" "ERROR"
	return 1
    fi 
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    SOURCE_LIST=`cat $freply`
    do_source $SGROUP
    return 0
}


##################################################################
# WORKDIR
do_workdir()
{
    if cat $SLAPT_RC | sed "s|WORKINGDIR=.*|WORKINGDIR=$WORKDIR|" > $SLAPT_RC.new; then
        mv -f $SLAPT_RC $SLAPT_RC.bak
	mv -f $SLAPT_RC.new $SLAPT_RC
    else
	msgbox "Cannot set WORKINGDIR" "ERROR"
	return 1
    fi
    grep -e "^WORKINGDIR=" $SLAPT_RC
}

## Ask a directory
menu_workdir()
{
WORKDIR=`grep -e "^WORKINGDIR=" $SLAPT_RC | cut -f2 -d=`
WORKDIR=${WORKDIR:-/home/ftp/pub/veclinux-$VERSION/packages}
mkdir -p $WORKDIR

TITLE="SELECT LOCAL WORKING DIR"
    if [ "$DISPLAY" ]; then
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --fselect \
	$WORKDIR/PACKAGES.TXT 0 0 2> $freply || return $?
    else
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --fselect \
	$WORKDIR/PACKAGES.TXT 10 60 2> $freply || return $?
    fi
    WORKDIR=`cat $freply`
    if [ ! -d "$WORKDIR" ]; then
	WORKDIR=`dirname $WORKDIR`
    fi
    do_workdir
}


##################################################################
# EXCLUDE
do_exclude()
{
    if cat $SLAPT_RC | sed "s|^EXCLUDE=.*|EXCLUDE=$EXCLUDE|" > $SLAPT_RC.new; then
        mv -f $SLAPT_RC $SLAPT_RC.bak
	mv -f $SLAPT_RC.new $SLAPT_RC
    else
	msgbox "Cannot set EXCLUDE option" "ERROR"
	return 1
    fi
}

menu_exclude()
{
EXCLUDE=`grep -e "^EXCLUDE=" $SLAPT_RC | cut -f2 -d=`
EXCLUDE=${EXCLUDE:-"kernel-ide,kernel-source,kernel-headers,kernel-modules"}

TITLE="ENTER EXCLUDE EXPRESSION"
TEXT="\n
In this edit box, you may enter regular expressions to exclude
unwanted packages. Separate the expressions using a comma.\n
Here are some examples of regular expressions:\n
- Package name, as is : kernel-headers\n
- All beta/alpha      : beta.*,alpha.*\n
- All kde or libraries: kde.*,lib.*\n
- All i586 and i686   : i[56]+86\n"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
      --inputbox "$TEXT" 18 76 $EXCLUDE 2> $freply || return $?

EXCLUDE=`cat $freply`
do_exclude

}


#########################################################################
menu_config()
{
DEF_ITEM="SOURCE"
while [ 1 ]; do
  DIMENSION="20 73 7"
  TITLE="CONFIGURATION"
  TEXT="\n
The following menu allow you to set slapt-get configuration.\n
Most importantly, you must set a VLocity Linux mirror source.\n
You may also set Slackware or other sources, but be careful because
the dependency between multiple sources is generally not guaranteed.\n
In case this menu does not up to the tasks, you may edit the\n
config file directly: $SLAPT_RC."

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--default-item $DEF_ITEM --menu "$TEXT" $DIMENSION \
"FAVOURITE" "Set favourite sources" \
"VLocity"    "Set VLocity Linux mirror sources" \
"SLACKWARE" "Set Slackware mirror sources" \
"OTHER"     "Set other mirror sources" \
"EXCLUDE"   "Set exclusion filter" \
"WORKDIR"   "Specify working directory" \
"DONE" "finish configuration" \
2> $freply

   status=$?
   [ $status != 0 ] && return $status

    REPLY=`cat $freply`
    case $REPLY in
      WORKDIR)
        menu_workdir
	DEF_ITEM="WORKDIR"
        ;;
      EXCLUDE)
        menu_exclude
	DEF_ITEM="EXCLUDE"
        ;;
      DONE)
        return 0
        ;;
      *)
        menu_source $REPLY
	DEF_ITEM="$REPLY"
        ;;
    esac
done
}


#########################################################################

menu_expert_doc() {
while [ 0 ]; do
DIMENSION="16 54 3"
TITLE="EXPERT WAY"
TEXT="\n
VLAPT is only a poor front-end for slapt-get.\n
To reveal the full power of this advanced package
tool, please work on a terminal or console then 
use the command line interface: slapt-get.\n\n
Pardon the inconvenience ;-)\n"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--menu "$TEXT" $DIMENSION \
"README" "Slapt-get README" \
"FAQ"    "Slapt-get FAQ" \
"DONE"	"Ok, thanks for the info" 2> $freply || return $?

case `cat $freply` in
    README)
	$DCMD --title "SLAPT-GET README" --textbox "$SLAPT_README" 22 76
	;;
    FAQ)
	$DCMD --title "SLAPT-GET FAQ" --textbox "$SLAPT_FAQ" 22 76
	;;
    DONE)
	return 0
	;;
esac
done
}


menu_expert()
{
SLAPT_DOC=`find /usr/doc -name slapt-get-*`
if [ "$SLAPT_DOC" ]; then
    SLAPT_README="$SLAPT_DOC/README"
    SLAPT_FAQ="$SLAPT_DOC/FAQ"
    if [ -f $SLAPT_FAQ ] && [ -f $SLAPT_README ]; then
	menu_expert_doc
	return 0
    fi
fi

DIMENSION="12 55"
TITLE="EXPERT"
TEXT="\n
VLAPT is only a poor front-end for slapt-get.\n
To reveal the full power of this advanced package
tool, please work on a terminal or console then 
use the command line interface: slapt-get.\n\n
Pardon the inconvenience ;-)\n"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--msgbox "$TEXT" $DIMENSION 
}

#########################################################################
menu_main()
{
DEF_ITEM="UPDATE"
while [ 1 ]; do
  DIMENSION="20 70 8"
  TITLE="VLocity LINUX PACKAGE MANAGEMENT"
  TEXT="\n
VLocity linux supports slapt-get advanced package tools.\n
You may need the CONFIG menu to change the setting first.\n
If the setting is right, do UPDATE once, then fell free to
UPGRADE, INSTALL, or REMOVE as you wish."

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--default-item $DEF_ITEM --menu "$TEXT" $DIMENSION \
"UPDATE"  "Get new packages info from sources" \
"UPGRADE" "Upgrade installed packages" \
"INSTALL" "Install new packages and their dependencies" \
"NODEP"   "Install packages without dependency checking" \
"REMOVE"  "Remove installed packages and the dependants" \
"CONFIG"  "Set sources, working directory, and filter" \
"EXPERT"  "Hints to do it the geeks way" \
"DONE"    "finish doing this" \
2> $freply

   status=$?
   [ $status != 0 ] && return $status

    case `cat $freply` in
      UPDATE)
        menu_update
	DEF_ITEM="UPGRADE"
	;;
      UPGRADE)
        menu_upgrade
	DEF_ITEM="INSTALL"
	;;
      INSTALL)
        menu_install
	DEF_ITEM="DONE"
	;;
      NODEP)
        menu_nodep
	;;
      REMOVE)
        menu_remove
	DEF_ITEM="DONE"
	;;
      EXPERT)
        menu_expert
	DEF_ITEM="DONE"
	;;
      CONFIG)
        menu_config
	DEF_ITEM="UPDATE"
	;;
      DONE)
        clean_exit 0
	;;
    esac
 done
}

####################################################################################
# MAIN PROGRAM

wizard menu_main
result=$?
if [ "$1" = "--super-vlapt" ]; then
   clean_exit 0
else
   clean_exit $result
fi

