#!/bin/sh
# This script randomly permutes the machines file
#
machinesFile=$1
machines_randFile=$2
#
machines_temp="$TEMPDIR/mpitemp-${LOGNAME}$$"
machines_temp2="$TEMPDIR/mpitemp2-${LOGNAME}$$"
#
cat $machinesFile | sed -e '/^#/d' -e 's/#.*^//g' > $machines_temp
#
if [ -f "$machines_temp" ] ; then
    machine_num=`wc -l $machines_temp | sed "s/\([\/]\).*//g"`
fi
#
while [ ${machine_num} -gt 0 ]
do 
   n=`expr $RANDOM % $machine_num`
   n=`expr ${n} + 1`
#
   if [ ${n} -eq 1 ] ; then   
       cat $machines_temp | head -1 >> $machines_randFile
       cat $machines_temp | tail -`expr ${machine_num} - 1` >> $machines_temp2
   elif [ ${n} -eq ${machine_num} ] ; then
       cat $machines_temp | tail -1 >> $machines_randFile
       cat $machines_temp | head -`expr ${n} - 1` >> $machines_temp2
   else
       cat $machines_temp | head -${n} | tail -1 >> $machines_randFile
       cat $machines_temp | head -`expr ${n} - 1` > $machines_temp2
       cat $machines_temp | tail -`expr ${machine_num} - ${n}` >> $machines_temp2
   fi
#
   mv $machines_temp2 $machines_temp
   machine_num=`expr ${machine_num} - 1`
done
#
/bin/rm -f $machines_temp $machines_temp2
