#!/bin/sh


if [ $# -ne 3 ];then
	echo "argument number must 3, with the format: labsub filenum corenum qname"
	echo "qname: main  mem  old"
	exit 0;
fi
if [ $# -eq 3 ];then
	qname=$3
fi
if [ $1 -gt 1000 ] && [ $1 -lt 1 ]; then

	echo "argument 1 should in 1-1000"
	exit 0
fi
if [ $2 -gt 12 ] && [ $2 -lt 1 ]; then
	echo "argument 2 should in 1-12"
	exit 0
fi

count=0
id=1
rm *.sh
group_number=$1
nodes=$2

find `pwd`|grep txt|while read i;do
	count=$((count+1))
	if [ $count = $id ] ; then 
		echo "#!/bin/sh"  >task$id.sh
	fi

	echo "calc.sh $i;" >>task$id.sh

	if [ $((count-id+1)) -eq $group_number ]; then
		id=$((count+1))
	fi
done

####
count=0
id=1

find `pwd` -name 'task*sh'>tempfile
while read i; do
	count=$((count+1))
	if [ $count = $id ] ; then 
		echo "#!/bin/sh"  >bundle$id.sh
	fi

	echo "sh $i&" >>bundle$id.sh

	if [ $((count-id+1)) -eq $nodes ]; then

    echo "wait">>bundle$id.sh
    bsub -q $qname -n $nodes bundle$id.sh
		id=$((count+1))
	fi
done <tempfile

if [ $id -le $count ]; then
  echo "wait">>bundle$id.sh
  bsub -q $qname -n $nodes bundle$id.sh
fi



