#!/bin/sh

#
# help() To print help
#
help()
{
  echo "Usage: $0 -r -[d|c]"
  echo "Options: The following are valid arguments"
  echo " -r name of reseller to filter for (optional)"
  echo " -d command to run per domain"
  echo " -c command to chroot and run per domain"
  echo "The following variables can be used in the command:"
  echo " <site>         the site number"
  echo " <admin>        the admin number"
  echo " <domain>       the domain name"
#  echo " <reseller_id>  the reseller name for the current domain"
  exit 1
}

#
#if no argument
#
if [ $# -lt 1 ]; then
  help
fi

reseller="-a"

while getopts r:d:c opt
do
	case "$opt" in
		d) command="$OPTARG";;
		c) chroot="chroot /home/virtual/<domain> ";;
		r) reseller=" -r $OPTARG";;
		\?) help;;
	esac
done

for Info in `sitelookup $reseller`
do
	Site=`echo $Info|cut -d, -f3`
        Admin=`echo $Info|cut -d, -f2`
	Domain=`echo $Info|cut -d, -f1`
	command2="$(echo $command | sed "s/<domain>/$Domain/g;s/<site>/$Site/g;s/<admin>/$Admin/g")"
	chroot2="$(echo $chroot | sed "s/<domain>/$Domain/")"
	$chroot2 $command2
#	$command
done
#echo 'Done!'

