#/bin/ksh -x

if [[ x$ATM_MODEL_VERSION == x ]] ; then
	 echo "ERROR: need to load a model first"
	 exit 1
fi

destdir=$1

if [[ ! -d $destdir ]] ; then
	 echo "ERROR: dir not found: $destdir"
	 echo "Usage: $0 mydir"
	 exit 1
fi
destdir=$(true_path $destdir)


#srclist="$(ls -d $gemdyn/src/* $rpnphy/src/* $modelutils/src/* $gemdyn/include $rpnphy/include $modelutils/include)"

srclist="gemdyn rpnphy modelutils"

flatdir=`pwd`
filelist="$(ls *.[cfh]*)"

for mysrc in $srclist ; do
	 cd $(eval "echo \$$(echo $mysrc)")/src
	 for mypart in $(ls -d *) ; do
		  mypart2=${mysrc%*/}
		  mypart2=${mypart2##/*}/$mypart
		  for myfile in  $filelist; do
				if [[ -r ./$mypart/$myfile ]] ; then 
					 [[ ! -d $destdir/$mypart2 ]] && mkdir -p $destdir/$mypart2 2>/dev/null
					 mv $flatdir/$myfile $destdir/$mypart2
				fi
		  done
	 done
done
