#!/bin/csh -f


if ($1 == -h || $1 == --help) then
echo "BEAMSIFT"
echo ""
echo "written by Luke Abraham 2005/10/21"
echo "run beamsift {file} {line spacing} to give desired effect"
exit 1
endif

set awk_script=beamsift.awk
set bindir=${HOME}/bin
set scriptdir=$bindir/.scripts/
set file_name=$1
set temp_file=$file_name.tmp
set s_num=`expr $2 - 1`
set file_length=`wc $file_name | awk '{print $1}'`

set i=1
set run_num=`expr ${file_length} \/ ${s_num}`
set rnm1=`expr $run_num - 1`

awk '{x=x+1; print x,$0}' $file_name > $temp_file

while($i<`expr $run_num + 1`) 
    set im1=`expr $i - 1`
    if (`expr $im1 \* $rnm1` > `expr $file_length`) then
	rm -f $temp_file
	echo "all files have been created"
	exit 1
    endif
    if ($i < 10 ) then
	echo "creating file "00${i}"..."
	awk -v iloop=$i -v set_num=$s_num -v fl=$file_length -f $scriptdir$awk_script $temp_file > $file_name.00${i}
    else if ($i < 100 && $i >= 10) then
	echo "creating file "0${i}"..."
	awk -v iloop=$i -v set_num=$s_num -v fl=$file_length -f $scriptdir$awk_script $temp_file > $file_name.0${i}
    else 
	echo "creating file "${i}"..."
	awk -v iloop=$i -v set_num=$s_num -v fl=$file_length -f $scriptdir$awk_script $temp_file > $file_name.${i}
    endif
set i=`expr $i + 1`
end

echo "all files have been created"
rm -f $temp_file
exit 1