#!/bin/sh
#
# Program to move files from a source directory to a destination directory
# and change the file ownership of the files to the user id specified.
#
#  Usage:   FileMover.sh SourceDirectory DestDirectory UserId
#
SourceDir="$1"
DestDir="$2"
UserId="$3"
BaseDir=`pwd`

case "$SourceDir" in
	/*)
	     : Source is a full path.
	;;
	*)
	     : Source is a relative path.
		 SourceDir=$BaseDir/"$SourceDir"
	;;
esac

case "$DestDir" in
	/*)
	     : Destination is a full path.
	;;
	*)
	     : Destination is a relative path.
		 DestDir=$BaseDir/"$DestDir"
	;;
esac


usage()
{
    echo "usage $0: SourceDir DestinationDir UserID"
    exit 1
}

if [ ! -d "$SourceDir" ]
then
    echo "The Source Directory is not valid"
    usage
fi

if [ ! -d "$DestDir" ]
then
    echo "The Destination Directory is not valid"
	usage
fi

su "$UserId" -c "exit 0" >/dev/null 2>&1

if [ "$?" != 0 ]
then
    echo "An invalid User ID was specified"
	echo " - or -"
	echo "You don't have proper permissions to run this"
    usage
fi

cd "$DestDir"
if [ $? != 0 ]
then
    echo "You don't have permissions for the Destination Directory"
    usage
fi
cd $BaseDir

cd "$SourceDir" >/dev/null 2>&1

if [ $? = 0 ]
then
    mv * "$DestDir" # change mv to cp if a copy is desired.
	cd $BaseDir
    cd "$DestDir"
    chown "$UserId" *
    chmod u+rw *
    chmod og-rwx *

    ls -l 
    echo "Move Complete"
else
    echo "You don't have permissions for the Source Directory".
    usage
fi

