#!/bin/sh
#
#    choice_fltr [ -F ] [-M msg] [-T tag] [-c #] infile outfile
#
#	Copies user selected lines from {infile} to {outfile}
#	-F : the flag to parsing bundle format input file
#	-M : use following message as prompt
#	-T : use following tag as the name of object being selected
#	-c : use following number as the number of columns to display
#
#
#############################################################################
#
#	Copyright (C) Cadence Design Systems, Inc. All rights
#	reserved.  Unpublished -- rights reserved under the
#	copyright laws of the United States of America.
#
#			RESTRICTED RIGHTS LEGEND
#	Use, duplication, or disclosure by the Government is subject
#	to restrictions as set forth in subparagraph (c)(l)(ii) of 
#	the Rights in Technical Data and Computer Software clause 
#	at DFARS 52.227-7013.
#
#			Cadence Design Systems, Inc.
#			555 River Oaks Parkway
#			San Jose, CA 95134	USA
#
#############################################################################
BOURNE=/bin/sh
XTERMPATH=/usr/bin/X11
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''

Clear() {
    clear		2>/dev/null	#beware of bad termcap entry
}

Page() {
    TERM=vt100 MORE=""  more		#make sure we don't clear screen
}

EchoN() {
    echo  "$*\c"
}

Beep() {
    tput bel		2>/dev/null
}


Dirname() {					#print argument minus leaf, or .
    case $1 in
	*/* )	echo $1 | sed 's@/[^/]*$@/@' ;;
	*)	echo "." ;;
    esac
}

Leafname() {					#print leaf
    case $1 in
	*/* )	echo $1 | sed 's@.*/@@' ;;
	*)	echo $1 ;;
    esac
}

Fullpath() {					#print rooted path to arg dir
    ( cd $1 && /bin/pwd |
	sed 's@^/tmp_mnt/@/@' )			#print automount access dir
}

Dirpath() {					#print rooted path to arg - leaf
    $jsr Fullpath `$jsr Dirname $1`
}

Suffix() {
    echo $1 | sed -n 's@.*\.@@p'
}

Prefix() {
    echo $1 | sed 's@\.[^.]*$@@'
}


Affirmative() {
    while : ; do
	read Affirm
	case $Affirm in
	    [yY]*) return 0 ;;
	    [nN]*) return 1 ;;
	    *)     $jsr EchoN "Please press y (yes) or n (no) : " ;;
	esac
    done
}

Proceed() {
    test "X$1" = 'X-q' || $jsr EchoN "Press [Return] to proceed, q to quit : "
    while : ; do
	read proc < /dev/tty
	case $proc in
	    "")    return 0 ;;
	    [qQ]*) return 1 ;;
	    *) 	   $jsr EchoN "Press [Return] to proceed, q to quit : " ;;
	esac
    done
}


numCols=2
msg1=
flg="f"

if [ "X$1" = "X-F" ]; then
   flg="F"

   shift
   field="printf(\"%-55.55s %6d %s \n\", \$1, \$4, \$7)"
   msg1="
     Product Description                           size(KB)      status
"
   numCols=1
else
   field="print \$1"
fi

if [ "X$1" = "X-M" ]; then
    shift
    msg="$1"; shift
else
    msg=
fi 

if [ "X$1" = "X-T" ]; then
    shift
    object="$1"; shift
else
    object=package
fi 

if [ "X$1" = "X-c" ]; then
    shift
    numCols="$1"; shift
fi 


infile=$1; shift
outfile=$1; shift

NL="
"

test -z "$bin_dir" && bin_dir=`$jsr Dirname $0`;	export bin_dir
process_file=$bin_dir/process_file
 

columnize=$bin_dir/columnize
inv_tapes="$BOURNE $bin_dir/inv_tapes"

gutterWidth=4
case $numCols in
    1)  columns="76";;
    2)  columns="36 36";;
    3)  columns="23 23 23";;
    *)  echo "$0 : -c Unknown columns"  1>&2;;
esac

all="[aA]*"
none="[nN]*"


while : ; do				#break out

#
# Give user options
#
    pkgcnt=`wc -l < $infile`
    #
    # Display objects in two columns
    #
    {
    echo "$msg"
    echo "$msg1"
    $process_file 4 p $Proot i $infile $flg "~"
    echo ${NL}
    echo "a) All of the above"
    echo "n) None of the above"
    test ! -z "$msg1" && echo "p) Previous Menu"
    } | $jsr Page
    #
    # get user selections
    #
    choiceFl=/tmp/ch$$

    $jsr EchoN "Type your choice (a,n,p,1-3,2 5 9 ...): "
    read choices
    echo "$choices" | tr ','  ' ' |\
	awk '{ for( i=1; i<=NF; i++ ) print $i }' |\
	sed 's@^\([0-9][0-9]*\)-\([0-9][0-9]*\)$@\1 \2@' > $choiceFl

    if grep '^n$' $choiceFl >/dev/null; then
	> $outfile
    elif grep '^a$' $choiceFl >/dev/null; then
	cp $infile $outfile
    elif grep '^p$' $choiceFl >/dev/null; then
        > $outfile ; exit 255	
    else			#actually go through the entries
	badEntries=/tmp/bad$$
	grep '[^0-9 ]' $choiceFl			> $badEntries
	if [ -s $badEntries ]; then
	    mv $choiceFl ${choiceFl}2
	    grep -v '[^0-9 ]' ${choiceFl}2 > $choiceFl
	    rm ${choiceFl}2

	    mv $badEntries ${badEntries}2
	    sed 's@$@	: illegal character@p' ${badEntries}2 > $badEntries
	    rm ${badEntries}2
	fi

	sort -n -o $choiceFl $choiceFl

	xtracts=/tmp/xtr$$
	last=0
	cat $choiceFl | while read beg end; do
	    if [ "$end" != "" ]; then
		if [ $beg -gt $end ]; then
		    echo "$beg-$end	: bad range"	>> $badEntries
		    continue
		fi 
		test $end -le $last && continue		#already did it
		test $beg -le $last && beg=`expr $last + 1`
		if [ $end -gt $beg ]; then		#print range
		    echo "${beg},${end}p"
		else
		    echo "${beg}p"
		fi
		last=$end
	    else
		if [ $beg -gt $pkgcnt ]; then
		    echo "$beg	: out of range"		>> $badEntries
		    continue
		fi
		test $beg -le $last && continue
		echo "${beg}p"
		last=$beg
	    fi
	done > $xtracts

	if [ -s $badEntries ]; then
	    echo "The following entries have been ignored"
	    $jsr Page < $badEntries
	    echo ""
	fi
	rm $badEntries

	if [ -s $xtracts ]; then
	    # if tooooo many selections, the following sed may blow up
	    sed -n -f $xtracts $infile > $outfile
	else
	    > $outfile
	fi
	rm $xtracts
    fi
    rm $choiceFl

    #
    # Do verification
    #
    if [ -s $outfile ]; then
	{
	    echo "${NL}You have selected the following ${object}s:${NL}"
            $process_file 4 p $Proot i $outfile $flg "~"  	
        } | $jsr Page
	$jsr EchoN "${NL}Are your selections correct? [y/n] "
    else
	echo "${NL}You have not selected any ${object}s.${NL}"
	$jsr EchoN "Is this correct? [y/n] "
    fi
    $jsr Affirmative && break

    echo "${NL}Reselect ${object}s.${NL}"
done
		
exit 0
