#!/bin/bash

# Script to move files into another directory up to the 650MB limit.

BURNDIR=$2;
SRCDIR=$1;
#echo $SRCDIR;
#LOOPME=`echo "y"`;
#TOTAL=0;

#echo "Determining files to move";
#TOTAL=$TOTAL+`ls -l|head -n 2|tail -n 1|awk '{print $5}'`;
#while [ $TOTAL -le 620000 ];
#do
#	#Calculate the files to include
#	if `ls -1|head -n 1 >> /temp/.cdrdir`; then
#		echo "added" `ls -1|head -n 1` "to list"
#		TOTAL=`echo `ls -l|head -n 2|tail -n 1|awk '{print $5}'``;
#	else 
#		echo "Error creating temp file";
#		exit 0;
#	fi
#done

echo "Moving files into burn directory";

# get something in there
	

#move the files
for i in $( ls -1 $SRCDIR );
#for i in $( cat /temp/.cdrdir );
do
	# move to the burn directory
	echo "Moving " `ls -1 $SRCDIR|head -n 1`;
	FILE=`ls $SRCDIR|head -n 1`;
#	if [ `du -s "$BURNDIR"|awk '{print $1}'` -le 650000 ] && [ `du -s "$SRCDIR/$FILE"|awk '{print $1}'` -le 40000 ]; then


# stuck here.  why wont it calc as integer?	SIZEDIF=690000-`du -s $BURNDIR|awk '{print $1}'`;
	if [ `du -s "$BURNDIR"|awk '{print $1}'` -le 690000 ] && [ `du -s "$SRCDIR/$FILE"|awk '{print $1}'` -le $SIZEDIF ]; then
		if cp "$SRCDIR/$FILE" "$BURNDIR/$FILE"; then
			echo $FILE "successfully moved.";
			rm -Rf "$SRCDIR/$FILE";
		else
			echo $FILE "had an error while moving.";
		fi
	else
		echo $FILE "Burn directory is full.";
		exit 0;
	fi
done
echo "Burn consolidation process complete.";
