#! /bin/sh

if [ "$1" = "-d" ]
then
    destdir=$2
    printfilesCmd=$3
    debug_state="-d"
else
    destdir=$1
    printfilesCmd=$2
    debug_state=""
fi

if [ "${destdir}" = "" -o "${printfilesCmd}" = "" ]
then
	echo "Usage: copyfiles [-d] destdir printfilesCmd"
	exit 1
fi

if [ ! -d ${destdir} ]
then
	echo "Error: destdir must be a directory"
	exit 1
fi

for file in `$printfilesCmd $debug_state`
do
	copyTo=${destdir}

        moduleDir=`dirname ${file}`
	srcDir=`dirname ${moduleDir}`
        packageDir=`dirname ${srcDir}`

        moduleDir=`basename ${moduleDir}`
        srcDir=`basename ${srcDir}`
        packageDir=`basename ${packageDir}`
        
        if [ `basename ${file} .py` != `basename ${file}` -o \
             `basename ${file} .pyz` != `basename ${file}` ]; then
            # The file is a Python file.  Is it in a src directory?
            if [ ${srcDir} = src ]; then
		copyPkg=${destdir}/${packageDir}
                copyTo=${copyPkg}/${moduleDir}
                (mkdir ${copyPkg}; touch ${copyPkg}/__init__.py) > /dev/null 2>&1
                (mkdir ${copyTo}; touch ${copyTo}/__init__.py) > /dev/null 2>&1
	    elif [ ${moduleDir} = pandac ]; then
                copyTo=${destdir}/pandac
                (mkdir ${copyTo}; touch ${copyTo}/__init__.py) > /dev/null 2>&1
            fi
        fi

	if cp -R ${file} ${copyTo}
	then
	    echo "copying ${file} to ${copyTo}"
	else
	    echo "ERROR: could not find ${file}"
	    exit 1
	fi
done

