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

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

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
   echo "vecpkg - Vector Linux package management using slapt-get"
   echo "usage : vecpkg"
   echo ""
   echo "For more information, please read 'man slapt-get'"
   echo "See /etc/slapt-get/slaptgetrc for the global configuration"
   exit 0
fi

if [ ! $UID = 0 ]; then
    exec $VDIR/vsuper "Super-VLAPT" "$0 --super-vlapt"
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/vector-version ]; then
    RELEASE=`cat /etc/vector-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
}


##################################################################
# SOURCE
do_source()
{
    cat $SLAPT_RC | while read LINE; do
	if 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
    done
    mv -f $SLAPT_RC $SLAPT_RC.bak
    mv -f $SLAPT_RC.new $SLAPT_RC
}

build_source_menu()
{
    grep -e "SOURCE=.*" $SLAPT_RC | while read LINE; do
	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
    done
}

menu_source()
{
TITLE="SOURCES"
TEXT="\n
Please select local directory, FTP site, or HTTP site for sources.\n
You should choose the nearest Vector Linux mirror site. You may also
use Slackware or other packages sources, but be careful because the
dependency between multiple sources is generally not guaranteed:"
DIMENSION="20 76 10"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    build_source_menu $fmenu
    echo '2> $freply' >> $fmenu

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

    SOURCE_LIST=`cat $freply`
    do_source
    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-header, aaa\n
- All beta/alpha      : beta.*,alpha.*\n
- All kde or libraries: kde.*,lib.*\n
- All i586 and i686   : i[56]+86\n
- All VL packages     : -?vl?"

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

EXCLUDE=`cat $freply`
do_exclude

}

##################################################################
# UPDATE, get info from sources
do_update()
{
    echo "Updating packages info, please wait ..." > $freply
    ( slapt-get -y --update 2>&1 | sed 's/\.\..*Done/\.\.\.Done/' | \
	sed 's/\.\..*Cached/\.\.\.Cached/' >> $freply
      echo "--------------------------------" >> $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 necessary information from the SOURCEs and stores it
as local cache. You need to do this if you changed the SOURCEs, or 
you think the SOURCEs have been upgraded since the last update.\n
We are about to update the packages 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" "ERROR"
	return 0
    fi
    echo '2> $freply' >> $fmenu

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

    do_update
    return 0
}

##################################################################
# UPGRADE
do_upgrade()
{
    ( 
    echo "Upgrading packages, please wait ..." > $freply
    for PKG in $PKG_LIST; do
	slapt-get -y --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $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 "It seems that your system is uptodate" "NO 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 ..." > $freply
    for PKG in $PKG_LIST; do
	slapt-get -y --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $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
    SIZE2=`slapt-get -s --install $PKG_CHOICE 2>/dev/null | grep -e "^After unpacking"`
if [ "$SUGGESTS_LIST" ]; then
TEXT="\n
About to install package $PKG_CHOICE.\n
$SIZE1\n
$SIZE2\n
Please add suggested packages as needed then hit <OK>:"
else
TEXT="\n
About to install package $PKG_CHOICE.\n
$SIZE1\n
$SIZE2\n
Press <OK> when you are ready:"
fi
TITLE="INSTALL PACKAGES"
DIMENSION="20 76 9"

    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 allows you to select and install a package and its
dependencies. You may narrow the selection by specifying:\n
- Package  : mplayer,office,firefox,...\n
- Group    : DEV,EDIT,EDU,ENT,GR,MM,NET,OFC,SET,SYS,UTIL,CORE,BASE\n
- Libraries: cli,x11,gtk,gtk2,gnome,qt,kde,sdl\n
Combine the terms as the following examples:\n
- Mplayer and xine         : (mplayer|xine)\n
- Entertainment only       : /ENT\n
- Graphics and Multimedia  : (/GR|/MM)\n
- Only x11 (graphics mode) : x11/\n
- Only kde and sdl (games) : (/kde|/sdl)\n
Just let this empty if you want to know 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_install1

[ $? = 1 ] && return 0

done

}
##################################################################
# INSTALL NODEP
do_nodep()
{
    ( 
    for PKG in $PKG_LIST; do
        echo "Installing package $PKG, please wait ..." > $freply
	slapt-get -y --no-dep --install $PKG 2>&1 | sed 's!\.\..*%!\.!' >> $freply
    done
    echo "--------------------------------" >> $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_CHOICE=`cat $freply`
    do_nodep

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

menu_nodep()
{
while [ 0 ]; do
TITLE="INSTALL PACKAGES NODEP"
TEXT="\n
This wizard allows you to select and install several packages without
dependency checking. First, specify the search expression:\n
- Package  : mplayer,office,firefox,...\n
- Group    : DEV,EDIT,EDU,ENT,GR,MM,NET,OFC,SET,SYS,UTIL,CORE,BASE\n
- Libraries: cli,x11,gtk,gtk2,gnome,qt,kde,sdl\n
Combine the terms as the following examples:\n
- Mplayer and xine         : (mplayer|xine)\n
- Entertainment only       : /ENT\n
- Graphics and Multimedia  : (/GR|/MM)\n
- Only x11 (graphics mode) : x11/\n
- Only kde and sdl (games) : (/kde|/sdl)\n
Empty expression 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
    slapt-get -y --remove $PKG_CHOICE 2>&1 | sed 's!\.\..*%!\.!' >> $freply 
    echo "--------------------------------" >> $freply
    echo "Press [Enter] to continue" >> $freply 
    ) &
    $DCMD --backtitle "$BACKTITLE" --title "REMOVE PACKAGE" \
    --tailbox $freply 20 74 2>/dev/null
}

build_remove1_menu()
{
    slapt-get -s --remove $PKG_CHOICE | grep "is to be removed" | while read LINE; do
	PLONG=`echo $LINE | cut -f1 -d ' '`
	PNAME=`pkgname $PLONG`
	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' \\" >> $freply
    done
}

menu_remove1()
{
SIZE=`slapt-get -s --remove $PKG_CHOICE | grep -e "^After unpacking"`
TEXT1="\n
About to remove package $PKG_CHOICE,
along with the following dependent packages !!!\n
$SIZE\n
Press <Cancel> if this is not what you want, otherwise hit <OK>:"

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

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

    do_remove
    return 0
}

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' \\" >> $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()
{
TITLE="REMOVE PACKAGES"
TEXT="\n
Please select the packages you want to remove:"
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_remove_menu; then
	msgbox "No package to be removed" "SORRY"
	return 1  
    fi
    echo '2> $freply' >> $fmenu

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

    PKG_CHOICE=`cat $freply`
    menu_remove1
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 Vector 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
}


#########################################################################
menu_main()
{
DEF_ITEM="UPDATE"
while [ 1 ]; do
  DIMENSION="20 70 10"
  TITLE="VECTOR LINUX PACKAGE MANAGEMENT"
  TEXT="
Vector linux packaging supports auto dependency using slapt-get.
Please set the necessary SOURCE and WORKDIR, and optional FILTER.
After that, simply UPDATE then UPGRADE or INSTALL. Enjoy."

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--default-item $DEF_ITEM --menu "$TEXT" $DIMENSION \
"SOURCE"   "Specify mirror site" \
"WORKDIR"  "Specify working directory" \
"FILTER"   "Set exclusion filter" \
"UPDATE"   "Get new packages info from mirrors" \
"UPGRADE"  "Upgrade installed packages" \
"INSTALL"  "Install new packages" \
"NODEP" "Install without dependency" \
"REMOVE"   "Remove installed packages" \
"MIRROR"   "Download packages and make local mirror" \
"DONE" "finish doing this" \
2> $freply

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

    case `cat $freply` in
      SOURCE)
        menu_source
	DEF_ITEM="WORKDIR"
        ;;
      WORKDIR)
        menu_workdir
	DEF_ITEM="UPDATE"
        ;; 
      FILTER)
        menu_exclude
	DEF_ITEM="UPDATE"
        ;;
      UPDATE)
        menu_update
	DEF_ITEM="UPGRADE"
	;;
      UPGRADE)
        menu_upgrade
	DEF_ITEM="INSTALL"
	;;
      INSTALL)
        menu_install
	DEF_ITEM="DONE"
	;;
      NODEP)
        menu_nodep
	DEF_ITEM="DONE"
	;;
      REMOVE)
        menu_remove
	;;
      MIRROR)
        menu_mirror
	;;
      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

