#!/bin/bash

# Command line tutorial: Moving a collection from Greenstone to DSpace

pushd $GSDL3SRCHOME
source ./gs3-setup.sh

coll=$1
sampleFiles=$2
prepForPreview=$3

tmp_dir=gs2build/tmp/incr_building
mkdir -p $tmp_dir

manifest_flag=
if [[ "$coll" == "incremen" ]]; then
    manifest_flag="-manifest manifests"
fi

#https://stackoverflow.com/questions/9139401/trying-to-embed-newline-in-a-variable-in-bash
regex1="\* 3 documents were considered for processing\s*
\* 3 were processed and included in the collection"

#regex2="0 documents were considered for processing"
regex2="\s*\* 0 documents were considered for processing\s*
\s*\* 0 were processed and included in the collection\s*"

regex3="\s*GreenstoneXMLPlugin: processing fb33fe.dir/doc.xml\s*
\s*GreenstoneXMLPlugin: processing b18ase.dir/doc.xml\s*"

regex4="\s*\* 2 documents were considered for processing\s*
\s*\* 2 were processed and included in the collection\s*"


assertOutputContainsRegex() {
    outputfile=$1
    regex=$2
    
    # https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash
    # grep -R "string" "filepath"
    
    buildoutput=`cat "${outputfile}"`
    if [[ " $buildoutput " =~ $regex ]]; then # space before and after the file content
	echo "@@@ $outputfile contains expected lines"
	echo "$regex"
    else
	echo "*********************************"
	echo "### $outputfile is missing lines:"
	echo "$regex"
	echo "*********************************"
	exit -1;
    fi
}

# This function is called each time after preparing for the next preview/browser test,
# So the script needs to properly exit to let GLI testing code run the browser testing
# for the current collection being incrementally built. This script will be invoked again
# to prep for the next preview of this collection until all 4 previews are then.
# Then the entire process starts again for the remaining 2 incrementally built collections.
# In total: 3 collections (incremen, autoincr, 2autoincr) with this script working towards
# preparing them for 4 coll previews each.
preppedForPreviewNumber(){
    popd
    exit $1
}

if [[ "$prepForPreview" == "1" ]]; then
    # full rebuild the first time
    perl -S full-rebuild.pl -site localsite $coll
    
    # just need to ensure the server is started once (the first time)
    # for all 3 incremental building collections
    if [[ "$coll" == "incremen" ]]; then
	./ant-logreset-restart-with-exts.sh
    fi
    
    # PREVIEW TEST: in browser, Search for kouprey: hit in b18ase. Search for groundnuts: no hits.
    preppedForPreviewNumber 1;
fi


if [[ "$prepForPreview" == "2" ]]; then
    # copy the folders fb33fe, fb34fe and wb34te in samplefiles/incr_build/more-files
    # into collection import folder at web/sites/localsite/collect/$coll/import
    cp -r $sampleFiles/incr_build/more-files/* $GSDL3HOME/sites/localsite/collect/$coll/import/.

    # work towards preview 2 will continue below, don't exit script yet
fi

# For the 2nd auto incremental tutorial, need to run the 2-in-1 script that
# does both incr import and build

if [[ "$coll" == "2autoinc" || "$coll" == "3solrinc" ]]; then
    if [[ "$prepForPreview" == "2" ]]; then
	# ADDING DOCS
	outputfile="$tmp_dir/${coll}1_add.txt"
	perl -S incremental-rebuild.pl -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex1"
	
	# PREVIEW TEST: groundnuts should have a hit in fb33fe
	preppedForPreviewNumber 2;
    fi

    if [[ "$prepForPreview" == "3" ]]; then
	# DELETING
	rm -rf "$GSDL3HOME/sites/localsite/collect/$coll/import/b18ase"
	rm -rf "$GSDL3HOME/sites/localsite/collect/$coll/import/fb33fe"
	
	outputfile="$tmp_dir/${coll}2_del.txt"
	perl -S incremental-rebuild.pl -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex2"
	assertOutputContainsRegex "$outputfile" "$regex3"
	
	# PREVIEW TEST: neither kouprey nor groundnuts should now be found
	preppedForPreviewNumber 3;
    fi

    if [[ "$prepForPreview" == "4" ]]; then
	# MODIFYING
	# Copy across the 2 modified files located in this scripts_and_patches folder
	cp $GSDL3SRCHOME/ext/testing/source/scripts_and_patches/mod_fb34fe.htm $GSDL3HOME/sites/localsite/collect/$coll/import/fb34fe/fb34fe.htm
	cp $GSDL3SRCHOME/ext/testing/source/scripts_and_patches/b20cre_metadata.xml $GSDL3HOME/sites/localsite/collect/$coll/import/b20cre/metadata.xml
	
	outputfile="$tmp_dir/${coll}3_mod.txt"
	perl -S incremental-rebuild.pl -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex4"

	# PREVIEW TEST: Search titles for GS3 and search full text for Greenstone
	preppedForPreviewNumber 4;
    fi

else    
    if [[ "$prepForPreview" == "2" ]]; then
	outputfile="$tmp_dir/${coll}1_import_add.txt"
    
	# STEP ONLY FOR "incremental with manifests" collection
	# copy the samplefiles/incr_build/manifests into incremen collection
	if [[ "$coll" == "incremen" ]]; then
	    cp -r $sampleFiles/incr_build/manifests $GSDL3HOME/sites/localsite/collect/$coll/.
	    manifest="${manifest_flag}/add-new-files.xml"
	fi
	# https://stackoverflow.com/questions/4181703/how-to-concatenate-string-variables-in-bash
	perl -S incremental-import.pl -incremental $manifest -site localsite $coll 2>&1 | tee $outputfile	
	
	assertOutputContainsRegex "$outputfile" "$regex1"
    
	outputfile="$tmp_dir/${coll}2_build_add.txt"
	perl -S incremental-buildcol.pl -activate -site localsite $coll 2>&1 | tee $outputfile
	
	# PREVIEW TEST: groundnuts should have a hit in fb33fe
	preppedForPreviewNumber 2;
    fi
    if [[ "$prepForPreview" == "3" ]]; then    
	# DELETION
	# for incremen collection, the manifest marks files that are deleted
	# for the autoincr and 2autoincr collections, we need to actually delete
	# those folders so their deletion can be detected by the incr build scripts
	if [[ "$coll" == "incremen" ]]; then
	    manifest="${manifest_flag}/delete-some-files.xml"
	else
	    rm -rf "$GSDL3HOME/sites/localsite/collect/$coll/import/b18ase"
	    rm -rf "$GSDL3HOME/sites/localsite/collect/$coll/import/fb33fe"
	fi
	
	outputfile="$tmp_dir/${coll}3_import_del.txt"
	perl -S incremental-import.pl -incremental $manifest -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex2"
	
	outputfile="$tmp_dir/${coll}4_build_del.txt"
	perl -S incremental-buildcol.pl -activate -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex3"
	
	# PREVIEW TEST: neither kouprey nor groundnuts should now be found
	preppedForPreviewNumber 3;
    fi

    if [[ "$prepForPreview" == "4" ]]; then
	# Copy across the 2 modified files located in this scripts_and_patches folder
	cp $GSDL3SRCHOME/ext/testing/source/scripts_and_patches/mod_fb34fe.htm $GSDL3HOME/sites/localsite/collect/$coll/import/fb34fe/fb34fe.htm
	cp $GSDL3SRCHOME/ext/testing/source/scripts_and_patches/b20cre_metadata.xml $GSDL3HOME/sites/localsite/collect/$coll/import/b20cre/metadata.xml
	
	
	# MODIFICATION: Modifying a document and a document's metadata
	if [[ "$coll" == "incremen" ]]; then
	    manifest="${manifest_flag}/mod-text-and-meta.xml"
	fi
	
	outputfile="$tmp_dir/${coll}4_import_mod.txt"
	perl -S incremental-import.pl -incremental $manifest -site localsite $coll 2>&1 | tee $outputfile
	
	assertOutputContainsRegex "$outputfile" "$regex4"
	
	outputfile="$tmp_dir/${coll}2_build_mod.txt"
	perl -S incremental-buildcol.pl -activate -site localsite $coll 2>&1 | tee $outputfile

	# PREVIEW TEST: Search titles for GS3 and search full text for Greenstone
	preppedForPreviewNumber 4;
    fi
fi

#popd
#exit 0


