#!/bin/sh

# This script normalizes the newlines in all files in
# a directory and adds the svn:eol-style property
# Author: Ben Wolfe - 10/2008
# http://blog.eflow.org

path=`pwd`

# get a flat list of all files under the current directory
f=`ls -RlQ | awk -v PATH=$path '{ \
if ($1 ~ /.*:$/) \
path = substr($1,3,length($1)-4); \
else \
if ($0 ~ /^-/) \
printf("%s%s/%s\n", PATH, path, \
substr($0, match($0,"\".*\"")+1, RLENGTH-2) \
) \
}'`

for i in $f
do
#ignore the build folder contents
if ! echo $i | grep build > /dev/null;
then
        #check subversion for this property
        x=`svn propget svn:eol-style $i`
        if [ ! -n "$x" ]
        then
                # get the file's extension
                ext=`echo "$i" | awk -F . '{print $NF}'`

                # This is an "if list contains" method for shell scripts
                # check if ext is one of the text based extensions
                case $ext in
                        java|jsp|css|js|xml|sql|txt|properties|tld|tag|html|htm|sh)
                                #echo No svn:eol-style prop: $i . Adding now.
                                dos2unix -a $i
                                svn propset svn:eol-style "CRLF" $i
                                ;;
                        *)
                                echo invalid extension: $ext
                                ;;
                esac
        fi
fi
done

echo "done"
