#! /bin/sh

###########################################
# Given a directory or a file as an argument, this 
# script converts the following files in the 
# directory to UNIX format by removing all 
# the bogey carraige returns.
###########################################

CWD=`pwd`

###########################################
# Useful functions
###########################################

#AEPC- echonl : no-new-line echo for Bourne shell
if [ "X`echo -n`" = "X-n" ]; then
  echonl() { echo ${1+"$@"}"\c"; }
else
  echonl() { echo -n ${1+"$@"}; }
fi


dos2unix ()
{
  verbose=0
  case $1 in 
    -v) 
	verbose=1
	shift
	;;
  esac

  if [ $# -ne 1 ] ; then 
	echo "usage: dos2unix [-v] filename"
	exit 1
  else
	cp $1 d2u_$$
	tr -d '\015' < d2u_$$ > $1 || exit 1
	rm d2u_$$
	[ $verbose -eq 1 ] && echo "Converted $1"
  fi
  return 0
}
  
 	

FilterPathName ()
{
  newpathname="$1"
  case "$newpathname" in
    ~*)
      if [ -f /bin/csh -a -x /bin/csh ] ; then
        newpathname=`/bin/csh -c "echo $newpathname"`
      fi
      ;;
    ../*|./*)
      newpathname=`pwd`/"${newpathname}"
      ;;
    *)
      ;;
  esac
  eval newpathname=\"${newpathname}\"
  echo "${newpathname}"
}

#############################################################

VERBOSE=0
case $1 in 
    -v) 
	VERBOSE=1
	shift
	;;
esac


if [ $# -ne 1 ] ; then
    echo "Usage: convert2unix [-v] filename|dirname"
    exit 1
fi



if [ -f $1 ] ; then
    case $1 in 
    *.c|*.h|*.conf|*akedrv|*akefile|*aster|*ystem|*aepinit*|*aepctrl*)
	[ $VERBOSE -eq 1 ] && dos2unix -v $1 || dos2unix $1
 	exit 0
	;;
    *)
	echo "$1 not recognised as suitable for conversion."
	exit 0
	;;
    esac
fi

DIRECTORY=`FilterPathName "$1"`

if [ -d $DIRECTORY ] ; then
    cd $DIRECTORY
    for file in `find . -type f -print` ; do
	case $file in 
	*.c|*.h|*.conf|*.log|*akefile|*akedrv|*aster|*ystem|*aepctrl*|*aepinit*)
	    dos2unix $file  
	    [ $VERBOSE -eq 1 ] && echo "Converted $file "
	    ;;
	*)
	    [ $VERBOSE -eq 1 ] && echo "Ignored   $file "
	    ;;
	esac
    done
    cd $CWD
else
   echo "$file is not a regular file or directory"
   exit 1
fi


exit 0


