#!/bin/sh

OUTPUT_PATH=$1
SVN_INFO=$2
SVN_PATH=$3
FILE_NAME=$4
OSBITS=$5
OS=$6
KEY=$7
EXCEPT=$8

#echo "Get installation files.... output svn_info svn_path filename osbits os key except" 
echo "### get value : $OUTPUT_PATH,$SVN_INFO,$SVN_PATH,$FILE_NAME,$OSBITS,$OS,$KEY,$EXCEPT"

mkdir -p $OUTPUT_PATH

echo "#### svn log -q  $SVN_INFO$SVN_PATH | grep \"^r[0-9]\" | awk -F\"|\" '{print $1}' | sed -e \"s|r||g\""
REVLIST=`svn log -q  $SVN_INFO$SVN_PATH | grep "^r[0-9]" | awk -F"|" '{print $1}' | sed -e "s|r||g"`
for REV in $REVLIST
do
	echo "##### svn log -r $REV -q -v $SVN_INFO$SVN_PATH | grep -v \"D /\" | grep \"$SVN_PATH/$FILE_NAME\" | awk '{print \$2}'"
	FILELIST=`svn log -r $REV -q -v $SVN_INFO$SVN_PATH | grep -v "D /" | grep "$SVN_PATH/$FILE_NAME" | awk '{print $2}'`
	for f in $FILELIST
	do
		LASTFILE=$f
		echo "###### Get file : $LASTFILE"
		if [ -n "$OSBITS" ]; then
			LASTFILE=`echo $LASTFILE | grep "$OSBITS"`
		fi
	
		if [ -n "$OS" ]; then
			LASTFILE=`echo $LASTFILE | grep  "$OS"`
		fi

		if [ -n "$KEY" ]; then
			LASTFILE=`echo $LASTFILE | grep "$KEY"`
		fi

		if [ -n "$EXCEPT" ]; then
			LASTFILE=`echo $LASTFILE | grep -v "$EXCEPT"`
		fi

		
		if [ -z  "$LASTFILE" ]; then
		else
			break
		fi

	done

	if [ -z "$LASTFILE" ]; then
	else
		break
	fi
	
done

echo "LASTFILE=[$LASTFILE]"
if [ -z "$LASTFILE" ]
then
	echo "error get SVN history"
	exit 1
fi

ERR=0

echo "#### svn export -q --force $SVN_INFO$LASTFILE $OUTPUT_PATH/$FILE_NAME"
svn export -q --force $SVN_INFO$LASTFILE $OUTPUT_PATH/$FILE_NAME
ERR=$?

if [ $ERR -ne 0 ]
then
	echo "error get tagging file"
	exit 1
fi
#echo "OK"
