#/bin/sh
#

#to have clucene produce packages (more than just default) set the following line to 1
#however, because of package protection, the constructor which is normally protected
#must be changed to public, therefore creating an ugly public constructor with no
#use. Unless you particuarly want proper java lucene like packages, leave this as 0
LUCENE_COMPATIBILITY_MODE=0

if test $LUCENE_COMPATIBILITY_MODE -eq 1; then
CLNAMESPACE=org.apache.lucene
else
CLNAMESPACE=clucene
fi

function SedAndDelete {
  #parameters:
  #$1: CLNAMESPACE
  #$2: file
  #$3: target folder
  
  if test $LUCENE_COMPATIBILITY_MODE -eq 1; then
		sed -e "9d" \
		-e "s/protected static long getCPtr/public static long getCPtr/" \
		-e "s/\(protected \)\(.*\)\((long cPtr, boolean cMemoryOwn)\)/public \2(long cPtr, boolean cMemoryOwn)/" \
		-e "1i\package $CLNAMESPACE.$1;" \
		-e "1i\import $CLNAMESPACE.analysis.*;" \
		-e "1i\import $CLNAMESPACE.document.*;" \
		-e "1i\import $CLNAMESPACE.index.*;" \
		-e "1i\import $CLNAMESPACE.queryParser.*;" \
		-e "1i\import $CLNAMESPACE.search.*;" \
		-e "1i\import $CLNAMESPACE.store.*;" \
		-e "1i\import $CLNAMESPACE.util.*;" \
		-e "1i\import $CLNAMESPACE.support.*;\n" \
		-e "1i\import CLjava.CLjavaJNI;\n" \
		$2 >java/$3$2
	else
		sed -e "9d" -e "1i\package $CLNAMESPACE;" \
		$2 >java/$3$2
	fi
	rm $2
}

function ChangeSupportClasses {
  #parameters:
  #$1: file

	if test $LUCENE_COMPATIBILITY_MODE -eq 1; then
		sed -e "1d" -e "2i\package $CLNAMESPACE.support;" -e "2,14s/import .*;//g" \
				-e "s/protected static long getCPtr/public static long getCPtr/" \
				-e "s/\(protected \)\(.*\)\((long cPtr, boolean cMemoryOwn)\)/public \2(long cPtr, boolean cMemoryOwn)/" \
				java/$1 >java/$1.tmp1
		sed -e "/^$/{
			N
			/^\n$/d
		}" java/$1.tmp1 >java/$1.tmp2
		sed -e "2i\import $CLNAMESPACE.analysis.*;" \
		-e "2i\import $CLNAMESPACE.document.*;" \
		-e "2i\import $CLNAMESPACE.index.*;" \
		-e "2i\import $CLNAMESPACE.queryParser.*;" \
		-e "2i\import $CLNAMESPACE.search.*;" \
		-e "2i\import $CLNAMESPACE.store.*;" \
		-e "2i\import $CLNAMESPACE.util.*;" \
		-e "2i\import $CLNAMESPACE.support.*;\n" \
		-e "2i\import CLjava.CLjavaJNI;\n" \
		java/$1.tmp2 >java/$1.tmp3
		
		rm java/$1 java/$1.tmp1 java/$1.tmp2
		mv java/$1.tmp3 java/$1
	else
		sed -e "1d" -e "2i\package $CLNAMESPACE;" -e "2,14s/import .*;//g" \
				-e "s/public static long getCPtr/public static long getCPtr/" \
				-e "s/\(public \)\(.*\)\((long cPtr, boolean cMemoryOwn)\)/protected \2(long cPtr, boolean cMemoryOwn)/" \
				java/$1 >java/$1.tmp1
		sed -e "/^$/{
			N
			/^\n$/d
		}" java/$1.tmp1 >java/$1.tmp2
		rm java/$1 java/$1.tmp1
		mv java/$1.tmp2 java/$1
	fi
}

#fix cpp bindings
sed -e "50,100000s/\<new\>/_CLNEW/g" -e "50,100000s/\(\<delete\>\) \(.*\);/_CLDECDELETE\(\2\);/g" _clucene_binding.cpp.tmp >_clucene_binding.cpp
rm _clucene_binding.cpp.tmp

#move-clean:
rm _CL_p_uint8_tArray.java
rm _CL_p_int32_tArray.java
rm _CL_p_p_lucene__index__IndexReaderArray.java
rm _CL_p_p_lucene__index__TermFreqVectorArray.java
rm _CL_p_p_lucene__search__SortFieldArray.java
rm _CL_p_p_lucene__search__SearchableArray.java

#move-store:
SedAndDelete store Directory.java org/apache/clucene/store/
SedAndDelete store InputStream.java org/apache/clucene/store/
SedAndDelete store OutputStream.java org/apache/clucene/store/
SedAndDelete store FSDirectory.java org/apache/clucene/store/
SedAndDelete store RAMDirectory.java org/apache/clucene/store/
SedAndDelete store RAMInputStream.java org/apache/clucene/store/
SedAndDelete store RAMOutputStream.java org/apache/clucene/store/

#move-document:
SedAndDelete document Document.java org/apache/clucene/document/
SedAndDelete document Field.java org/apache/clucene/document/
SedAndDelete document DateField.java org/apache/clucene/document/
SedAndDelete document DocumentFieldEnumeration.java org/apache/clucene/document/

#move-util:
SedAndDelete util Reader.java org/apache/clucene/util/
SedAndDelete util BitSet.java org/apache/clucene/util/
SedAndDelete util BitVector.java org/apache/clucene/util/
SedAndDelete util FileReader.java org/apache/clucene/util/
SedAndDelete util StringReader.java org/apache/clucene/util/
SedAndDelete util Debug.java org/apache/clucene/util/

#move-analysis:
SedAndDelete analysis Analyzer.java org/apache/clucene/analysis/
SedAndDelete analysis Token.java org/apache/clucene/analysis/
SedAndDelete analysis TokenStream.java org/apache/clucene/analysis/
SedAndDelete analysis WhitespaceAnalyzer.java org/apache/clucene/analysis/
SedAndDelete analysis PerFieldAnalyzerWrapper.java org/apache/clucene/analysis/
SedAndDelete analysis SimpleAnalyzer.java org/apache/clucene/analysis/
SedAndDelete analysis StopAnalyzer.java org/apache/clucene/analysis/
SedAndDelete analysis.standard StandardAnalyzer.java org/apache/clucene/analysis/standard/

#move-index:
SedAndDelete index Term.java org/apache/clucene/index/
SedAndDelete index TermDocs.java org/apache/clucene/index/
SedAndDelete index TermPositions.java org/apache/clucene/index/
SedAndDelete index TermEnum.java org/apache/clucene/index/
SedAndDelete index IndexReader.java org/apache/clucene/index/
SedAndDelete index DocumentWriter.java org/apache/clucene/index/
SedAndDelete index IndexWriter.java org/apache/clucene/index/
SedAndDelete index MultiReader.java org/apache/clucene/index/
SedAndDelete index TermFreqVector.java org/apache/clucene/index/
SedAndDelete index SegmentInfo.java org/apache/clucene/index/
SedAndDelete index SegmentInfos.java org/apache/clucene/index/
SedAndDelete index SegmentReader.java org/apache/clucene/index/
SedAndDelete index SegmentMerger.java org/apache/clucene/index/
SedAndDelete index CompoundFileReader.java org/apache/clucene/index/
SedAndDelete index CompoundFileWriter.java org/apache/clucene/index/

#move-search:
SedAndDelete search Similarity.java org/apache/clucene/search/
SedAndDelete search Hits.java org/apache/clucene/search/
SedAndDelete search Filter.java org/apache/clucene/search/
SedAndDelete search HitDoc.java org/apache/clucene/search/
SedAndDelete search DateFilter.java org/apache/clucene/search/
SedAndDelete search Query.java org/apache/clucene/search/
SedAndDelete search Explanation.java org/apache/clucene/search/
SedAndDelete search Searchable.java org/apache/clucene/search/
SedAndDelete search Searcher.java org/apache/clucene/search/
SedAndDelete search Sort.java org/apache/clucene/search/
SedAndDelete search SortField.java org/apache/clucene/search/
SedAndDelete search IndexSearcher.java org/apache/clucene/search/
SedAndDelete search MultiSearcher.java org/apache/clucene/search/
SedAndDelete search TermQuery.java org/apache/clucene/search/
SedAndDelete search BooleanQuery.java org/apache/clucene/search/
SedAndDelete search PhraseQuery.java org/apache/clucene/search/
SedAndDelete search MultiTermQuery.java org/apache/clucene/search/
SedAndDelete search WildcardQuery.java org/apache/clucene/search/
SedAndDelete search RangeQuery.java org/apache/clucene/search/
SedAndDelete search PrefixQuery.java org/apache/clucene/search/

#move-queryParser:
SedAndDelete queryParser QueryParser.java org/apache/clucene/queryParser/

#support classes
if test $LUCENE_COMPATIBILITY_MODE -eq 1; then
	sed -e "s/class CLjavaJNI/public class CLjavaJNI/" \
	    -e "s/}/static {System.loadLibrary(\"CLjava\");} }/" \
		CLjavaJNI.java \
		>java/CLjavaJNI.java
	rm CLjavaJNI.java
else
	SedAndDelete support CLjavaJNI.java org/apache/clucene/support/
fi

mv -f _clucene_binding.cpp native/

ChangeSupportClasses org/apache/clucene/support/_CL_p_int32_tArray.java
ChangeSupportClasses org/apache/clucene/support/_CL_p_p_lucene__index__IndexReaderArray.java
ChangeSupportClasses org/apache/clucene/support/_CL_p_p_lucene__index__TermFreqVectorArray.java
ChangeSupportClasses org/apache/clucene/support/_CL_p_p_lucene__search__SearchableArray.java
ChangeSupportClasses org/apache/clucene/support/_CL_p_p_lucene__search__SortFieldArray.java
ChangeSupportClasses org/apache/clucene/support/_CL_p_uint8_tArray.java
