#!/bin/bash
#@DOC@ run wg on 1 or more files
#
# Copyright (C) 2006  Nathan (Acorn) Pooley
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License (gpl.txt) for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# You can contact the author, Nathan (Acorn) Pooley, by writing
# to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
# or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
# 

_usage() {
	echo "Usage: wgg [OPTIONS] <file1> ..."
	echo "  run wg in bacground for 1 or more files"
	wg -?
	exit 1
}

ocnt=0
olist[0]=""
unset olist

fcnt=0
flist[0]=""
unset olist

OPT_all=0

_addopt() {
	olist[$ocnt]="$1"
	(( ocnt = ocnt + 1 ))
}

_addfile() {
	flist[$fcnt]="$1"
	(( fcnt = fcnt + 1 ))
}

for i in "$@"
do
	if [[ x${1%\?}y = x-y ]] ; then
		_usage
	fi
	if [[ $i = -* ]] ; then
		_addopt "$i"
	elif [[ $i = a ]] ; then
		OPT_all=1
	else
		_addfile "$i"
	fi
done

if (( OPT_all )) ; then
	for i in wpic*.txt
	do
		_addfile "$i"
	done
fi

if (( fcnt < 1 )) ; then
	PS3="Choose--> "
	select i in wpic*.txt pic*/wpic*.txt all
	do
		if [[ $REPLY = a || $i = all ]] ; then
			OPT_all=1
			for j in wpic*.txt pic*/wpic*.txt
			do
				_addfile "$j"
			done
			break
		fi
		if [[ $REPLY = [qQ] ]] ; then
			exit 0
			break
		fi
		if [[ -f $i ]] ; then
			wg "${olist[@]}" "$i" &
		fi
	done
fi

for i in "${flist[@]}"
do
	wg "${olist[@]}" "$i" &
done
