#! /bin/bash

#CMS - CMS Made Simple
#(c)2004 by Leendert Meyer <leen.meyer@home.nl>
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

D="$(dirname $0)"
[ "$D" = "." ] && D="$(pwd)"

VERSION="${0##*/} Version 0.1"

USAGE="Usage: ${0##*/} -f|--force | -n|no-commit | -t|--test | -h|--help | -u|--user | -v|--version | -U|--usage [path]"

HELP="This script sets predefined properties for predefined mime-types in a svn repository:

text files  -> svn:eol-style native
executables -> svn:executable
.png files  -> svn:mime-type image/png
.gif files  -> svn:mime-type image/gif
.jpg files  -> svn:mime-type image/jpeg
.jpeg files -> svn:mime-type image/jpeg

What mime-types text-files and executables consist of is defined in the find-mime script.
"

ARGS=""
TEST=""
COMMIT="no"
while [ $# -gt 0 ]; do
  case $1 in
    -f|--force)
		ARGS="$ARGS $1"
		shift
		;;
    -t|--test)
		ARGS="$ARGS $1"
		TEST="echo"
		shift
		;;
    -n|--commit)
		COMMIT="yes"
		shift
		;;
    -u|--user)
		USERNAME="$2"
		shift 2
		;;
    -U|--usage)
  		echo "$USAGE"
		exit 1
		;;
    -v|--version)
  		echo "$VERSION"
		exit 1
		;;
    -h|--help)
  		echo "$VERSION"
		echo
  		echo "$USAGE"
		echo
  		echo "$HELP"
		exit 1
		;;
  esac
done

find "${1-.}" -type f -name \*.php | xargs -i $D/svn-propset-file $ARGS svn:keywords Id {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Added Id keyword to text files"

$D/find-mime --text "${1-.}" | xargs -i $D/svn-propset-file $ARGS svn:eol-style native {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Set properties of text files to svn:eol-style native"

$D/find-mime --exec "${1-.}" | xargs -i $D/svn-propset-file $ARGS svn:executable '' {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Set properties of executables to svn:executable"

$D/find-mime --bin "${1-.}" | grep '\.png$' | xargs -i $D/svn-propset-file $ARGS svn:mime-type 'image/png' {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Set properties of .png files to svn:mime-type image/png"

$D/find-mime --bin "${1-.}" | grep '\.gif$' | xargs -i $D/svn-propset-file $ARGS svn:mime-type 'image/gif' {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Set properties of .gif files to svn:mime-type image/gif"

$D/find-mime --bin "${1-.}" | grep '\.jpe?g$' | xargs -i $D/svn-propset-file $ARGS svn:mime-type 'image/jpeg' {}
[ "$COMMIT" = "yes" ] && $TEST svn commit ${USERNAME+--username "$USERNAME" }-m "Set properties of .jpeg and .jpg files to svn:mime-type image/jpeg"

