#!/bin/bash

# get the plugins ...
# parameters
#    -p --product <product> [OSBP, OS.bee], default OS.bee
#	 -r <repo>  [osbee, daily, mavenizing, master, iward-master-clean]
# 	 -ph <phase>   [<empty>, all, collect, compare]

paramProduct="OSBP"
paramRepo="daily"
paramPhase=""

LOCAL_DATE=`date +%Y-%m-%d-%H-%M-%S`

while [ $# -ge 1 ] ; do
	key="$1"
	case $key in
		-p|--product)
		paramProduct="$2"
		shift
		;;
		-r|--repo)
		paramRepo="$2"
		shift
		;;
		-ph|--phase)
		paramPhase="$2"
		shift
		;;
		*)
		echo "Unrecognized option: $key"
		echo "Valid options: -r <repo> (default: daily), -p <product> [OSBP, OS.bee] (default: OSBP), -ph <phase> [all, collect, compare] (all optional)"
		exit 1
		;;
	esac
	shift
done

if [ $paramProduct != "OSBP" ] && [ $paramProduct != "OS.bee" ]
then
	echo "wrong parameter product: $paramProduct - please use one of [OSBP, OS.bee]"
	exit 1
fi


extractdir=~/tmp/extract
licdir=~/tmp/with-licenses
targetdir=`pwd`
searchpattern=".*[i\|I][c\|C][e\|E][n\|N][s\|S][e\|E].*\|.*about\.htm.*|MANIFEST\.MF"


sourceRepoBase=http://10.131.1.10/downloads/p2/
sourceRepoBuild=$paramRepo/
sourceRepoPostfix=luna/latest/
sourceRepoURL=$sourceRepoBase$sourceRepoBuild$sourceRepoPostfix


if [ "$paramProduct" == "OSBP" ] 
then
	sourceSWF=org.eclipse.osbp.softwarefactory
	outFileLics=osbp-used-lics
	outFileBundles=osbp-used-bundles
	productName=OSBP
else	
	sourceSWF=net.osbee.softwarefactory
	outFileLics=osbee-used-lics
	outFileBundles=osbee-used-bundles
	productName=OS.bee
fi

function rmstruct() {
    local toberemoved=$1

    echo "rm $toberemoved"
    rm -r -f "$toberemoved"
    
    if [ -e "$toberemoved/META-INF/MANIFEST.MF" ]
    then
        # remove manifest as soon as something has been found, MANIFEST is the last chance to find the license
        rm -r -f $d/META-INF/MANIFEST.MF
    fi

    while [ "$toberemoved" != "." ]
    do
        toberemoved=${toberemoved%/*}
        if [ -e "$toberemoved" ]
        then 
            if [ "$toberemoved" != "." ]
            then
                echo "rmdir $toberemoved"
                # works only for empty dirs ... 
                rmdir "$toberemoved"
                if [ $(($?)) ]
                then
                    break
                fi
                #sleep 1
            fi
        else
            break
        fi
    done
}


function removeUnwantedFiles() {
    local d=$1

    for pattern in "\.class" "\.java" "\.png" "\.gif" "\.xml" "pde_shared_license\.htm" "ILicense\.html" "AcceptLicensesWizardPage\.html" "LicenseManager\.html" "THIRD-PARTY\.properties" "README" ".*\.source.*" "\.jar"
    do
        if [ "$d" != "${d%$pattern}" ]
        then
           rmstruct "$d"
        fi
    done
}


function collectBundlesFromP2() {

    local p2=$sourceSWF.p2

    wget -r --cut-dirs 7 $sourceRepoURL$p2
    if [ "$?" != "0" ]
    then
        echo "can not download bundles from $sourceRepoURL$p2.... $?"
        exit 1
    fi

    p2=$sourceSWF.target.p2
    wget -r --cut-dirs 7 $sourceRepoURL$p2
    if [ "$?" != "0" ]
    then
        echo "can not download bundles from $sourceRepoURL$p2.... $?"
        exit 1
    fi

    find . -name "content.jar" -type f -exec rm -r -f "{}" \;
    find . -name "artifacts.jar" -type f -exec rm -r -f "{}" \;
    find . -name "index.html*" -type f -exec rm -r -f "{}" \;
    find . ! -wholename "*/plugins/*" -type f -exec rm -r -f "{}" \;
    find . -name "**.jar" -type f -exec mv "{}" . \;
}

if [ "$paramPhase*" == "*" ] || [ "$paramPhase*" == "all*" ]
then
    echo "clean up $extractdir ... "
    
    rm -r -f $extractdir
    mkdir -p $extractdir

    echo "clean up $licdir ... "
    rm -r -f $licdir
    mkdir -p $licdir

    find $targetdir -wholename "*/packages/*" ! -wholename "*/ignoreFiles*" -type f -exec rm -r -f "{}" \;
    find $targetdir -wholename "*/packages/*" ! -wholename "*/ignoreFiles*" -type d -exec rmdir --ignore-fail-on-non-empty "{}" \;
    find $targetdir -wholename "*/packages/*" ! -wholename "*/ignoreFiles*" -type d -exec rmdir --ignore-fail-on-non-empty "{}" \;

    echo "collect the jars ..."

    echo "extract the license info from $extractdir"
    cd $extractdir
    pwd

    collectBundlesFromP2

    pwd
    for d in *.jar
    do

        mv $d $d.tmp
        # check if we decided about the jar already
        dsearchpattern=${d/_.*\.jar/_\*\.jar}
        exists=`find $targetdir -wholename "*/packages/$dsearchpattern" -print | grep -c $d`
        if [ "$exists" == "0" ]
        then
            echo "extracting $d ..."
            unzip -q "$d.tmp" -d "$d" 
        fi 
        rm "$d.tmp"
    done
fi

if [ "$paramPhase*" == "*" ] || [ "$paramPhase*" == "collect*" ] || [ "$paramPhase*" == "all*" ]
then
    rm -r $targetdir/no-info

    echo on
    cd $extractdir
    for d in *.jar
    do
        # check if we decided about the jar already
        exists=`find $targetdir -wholename "*/packages/$d" -type d -print | grep -c $d`
        if [ "$exists" == "0" ]
        then
            echo "find $extractdir/$d -regextype sed -regex '$searchpattern' -type f -print"
            find . -regextype sed -regex "$searchpattern" -type f -exec cp -p --verbose --parents "{}" "$licdir/" \;
            removeUnwantedFiles "$licdir/$d"
            cnt_files=`find "$licdir/$d" -wholename "*$d*" -type f | grep -c $d`
            if [ $cnt_files == 0 ]
            then
                rm -r -f "$licdir/$d"
            fi
            if [ ! -d "$licdir/$d" ]
            then
                echo "no lic info found for $d"
                echo " "
                mkdir -p "$targetdir/no-info/$d"
                echo "There was no license info in the bundle that could be used for the decission. Have to use the name." >"$targetdir/no-info/$d/decidedByName.txt"
            fi
        fi
    done
fi

if [ "$paramPhase*" == "*" ] || [ "$paramPhase*" == "collect*" ] || [ "$paramPhase*" == "compare*" ] || [ "$paramPhase*" == "all*" ]
then
    cd "$licdir"
    echo "sort the license info into the target dir"


    echo "find . -regextype sed -regex '$searchpattern' -type f -print | sort"

    found=`find . -regextype sed -regex "$searchpattern" -type f -print | sort`
    echo .
    echo $found
    echo .
    for d in $found
    do

        echo "check $d"
        removeUnwantedFiles "$d"

        if [ ! -e "$d" ]
        then
            continue
        fi 


        for l in `find $targetdir -iwholename "*sampleText*" -type f -print | sort`
        do
            if [ ! -e "$l" ]
            then
                continue
            fi 
            #echo "compare $d with $l" 
            diff -i -E -b -w -B -a -q "$d" "$l" 
            diffresult=$(($?))
            #echo "---- $diffresult "
            if [ $diffresult == 2 ]
            then
              pwd
              #echo "diff -i -E -b -w -B -a -q \"$d\" \"$l\" failed "
              #sleep 2
              break
            fi
            if [ $diffresult == 0 ]
            then
                echo "."
                echo "FOUND License Template!"
                echo "."
                targetsubdir=${l%sampleText*}packages
                mkdir -p $targetsubdir
                echo "cp --parent \"$d\" $targetsubdir/"
                cp --parent "$d" "$targetsubdir/"
                bundlename=${d/\.\//}
                bundlename=${bundlename%/}
                echo "cp $l \"$targetsubdir/$bundlename/\""
                cp $l "$targetsubdir/$bundlename/"
                rmstruct "./$d"
                echo "."
                break
            fi
        done
    done
fi


cd   $targetdir/no-info/
echo "checking `pwd` for known licenses ...."
for d in  *.jar
do
    dsearchpattern=${d/_*.jar/_*.jar}
    dgreppattern=${d/_*.jar/}
    echo "find $targetdir -type d -wholename \"*/sampleText*/$dsearchpattern\" -print | grep -c \"$dgreppattern\""
    exists=`find $targetdir -type d -wholename "*/sampleText*/$dsearchpattern" -print | grep -c "$dgreppattern"`
    if [ "$exists" == "0" ]
    then
        echo "not found $d:  $exists"
    else
        targetsubdir=`find $targetdir -type d -wholename "*/sampleText*/$dsearchpattern" -print | grep -m 1 "$dgreppattern"`
        targetsubdir=${targetsubdir/sampleText*/packages}
        echo "found $d:  $exists -> copy to $targetsubdir "
        mkdir -p $targetsubdir
        echo "mv \"$targetdir/no-info/$d\" $targetsubdir/"
        echo "Searchpattern: $dsearchpattern" >>"$targetdir/no-info/$d/decidedByName.txt"
        echo "Comparepattern: $dgreppattern" >>"$targetdir/no-info/$d/decidedByName.txt"
        mv "$targetdir/no-info/$d" $targetsubdir/
        rm -r -f $targetdir/no-info/$d
    fi
done

cd   $targetdir/no-info/
for d in  *.jar
do
    dsearchpattern=${d/_*.jar/_*.jar}
    dgreppattern=${d/_*.jar/}
    echo "find .. -type d -wholename \"*/packages/$dsearchpattern\" -print | grep -c \"$dgreppattern\""
    exists=`find .. -type d -wholename "*/packages/$dsearchpattern" -print | grep -c "$dgreppattern"`
    if [ "$exists" == "0" ]
    then
        echo "not found $d:  $exists"
        continue
    else
        echo "found $d:  $exists -> rm -r -f `pwd`/$d "
        rm -r -f `pwd`/$d 
    fi
done

cd $targetdir

echo "build $outFileLics.csv"
find . -wholename "*/packages/*" -type d | sed "s/\// /g" | sort | awk "{ printf \"%s;%s\n\",\$2,\$4 }"  >$outFileLics.csv

echo "build $outFileLics.html"
echo "<!DOCTYPE html><HEAD>" >$outFileLics.html
echo "<base href=\"http://dev.osbee.org/\">" >>$outFileLics.html
echo "<meta name=\"generator\" content=\"TYPO3 CMS\">" >>$outFileLics.html
echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3temp/Assets/8074a92758.css?1467724030\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/sr_feuser_register/Resources/Public/StyleSheets/default.css?1460031874\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/iconfont/Resources/Public/Lib/font-awesome/css/font-awesome.min.css?1458828660\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/contentpackage/Resources/Public/css/screen.css?1467792250\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://fonts.googleapis.com/css?family=Quicksand:400,700,300\" media=\"all\">" >>$outFileLics.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css\" media=\"all\">" >>$outFileLics.html
echo "</HEAD>" >>$outFileLics.html
echo "<div class=\"ce-table inner small\"><table><caption>$productName used licenses</caption><thead><td align=\"right\">#<td>License<td>Bundle</thead>" >>$outFileLics.html

find . -wholename "*/packages/**\.jar" -type d | sed "s/\// /g"  | sort | awk "BEGIN {oldbundle=\"\";i=0}{ lic=\$2; bundle=\$4==\$oldbundle?\"\":\$4; i+=\$4==\$oldbundle?0:1; print \"<tr><td align=\\\"right\\\"> \" \$i \". <td><a href=\\\"\"\$lic\"/sampleText/\\\"  target=\\\"_blank\\\">\"\$lic\"</a><td>\"\$bundle\"</tr>\"; oldlic=\$2; oldbundle=\$4 }" >>$outFileLics.html
echo "</table></div>" >>$outFileLics.html
echo "<p><div class=\"ce-table inner small\">last updated: $LOCAL_DATE</div></p>" >>$outFileLics.html


echo "build $outFileBundles.html"
echo "<!DOCTYPE html><HEAD>" >$outFileBundles.html
echo "<base href=\"http://dev.osbee.org/\">" >>$outFileBundles.html
echo "<meta name=\"generator\" content=\"TYPO3 CMS\">" >>$outFileBundles.html
echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3temp/Assets/8074a92758.css?1467724030\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/sr_feuser_register/Resources/Public/StyleSheets/default.css?1460031874\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/iconfont/Resources/Public/Lib/font-awesome/css/font-awesome.min.css?1458828660\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"typo3conf/ext/contentpackage/Resources/Public/css/screen.css?1467792250\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://fonts.googleapis.com/css?family=Quicksand:400,700,300\" media=\"all\">" >>$outFileBundles.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css\" media=\"all\">" >>$outFileBundles.html
echo "</HEAD>" >>$outFileBundles.html
echo "<div class=\"ce-table inner small\"><table><caption>OS.bee used licenses</caption><thead><td align=\"right\">#<td>Bundle<td>License</thead>" >>$outFileBundles.html

find . -wholename "*/packages/*\.jar" -type d | sed "s/\// /g"  | sort | sort -k 3 | awk "BEGIN {oldlic=\"\";i=1}{ lic=\$2==\$oldlic?\"\":\$2;bundle=\$4;i+=\$2==\$oldlic?0:1; print \"<tr><td align=\\\"right\\\"> \" i \". <td>\"\$bundle\"<td><a href=\\\"\"\$lic\"/sampleText/\\\"  target=\\\"_blank\\\">\"\$lic\"</a></tr>\" ;oldlic=\$2;oldbundle=\$4}" >>$outFileBundles.html
echo "</table></div>" >>$outFileBundles.html
echo "<p><div class=\"ce-table inner small\">last updated: $LOCAL_DATE</div></p>" >>$outFileBundles.html


echo done ...


