#!/bin/bash

function header (){

printf "\n\n************************************************************************************************************\n"
printf "$1"
printf "\n************************************************************************************************************\n\n"
}

function print (){
printf '%-15s : %s\n' "${1}" "${2}"
}


repo_path="/Users/amc.admin/Desktop/jmc/application"
mod_config="`pwd`/jmc.mods"
#-----------------------------------------------#
#Delete Existing Report
#-----------------------------------------------#

if [[ ! -e ../reports ]]; then
    echo "Report Does not Exist...!!"
else
    rm -rf ../reports/*
fi

if [[ ! -e ./reports ]]; then
    mkdir reports
else
	rm -rf reports/*
fi

if test -e "${repo_path}/target/site/images"; then
    cp -r "${repo_path}/target/site/images" "`pwd`/reports/"
fi
if test -e "${repo_path}/target/site/css"; then
    cp -r "${repo_path}/target/site/css" "`pwd`/reports/"
fi

while IFS= read -r module
do

  header "${module}"
  mkdir "reports/${module}"

if test -f "${repo_path}/${module}/target/spotbugsXml.xml"; then
    cp "${repo_path}/${module}/target/spotbugsXml.xml" "`pwd`/reports/${module}/"
    print "Action"  "Copy"
    print "File" "spotbugsXml.xml"
    print "From" "${repo_path}/${module}/target"
    print "To" "`pwd`/reports/${module}/"
fi

if test -f "${repo_path}/${module}/target/site/spotbugs.html"; then
    cp "${repo_path}/${module}/target/site/spotbugs.html" "`pwd`/reports/${module}/"
    print "Action"  "Copy"
    print "File" "spotbugs.html"
    print "From" "${repo_path}/${module}/target"
    print "To" "`pwd`/reports/${module}/"
fi

if test -e "${repo_path}/${module}/target/site/images"; then
    cp -r "${repo_path}/${module}/target/site/images" "`pwd`/reports/${module}/"
fi

if test -e "${repo_path}/${module}/target/site/css"; then
    cp -r "${repo_path}/${module}/target/site/css" "`pwd`/reports/${module}/"
fi

done < "${mod_config}"

if test -e "./summary.html"; then
    rm summary.html
fi

javac ResultParser.java
java ResultParser `pwd` summary  > summary.html
java ResultParser `pwd` xml      > xml_list.html
java ResultParser `pwd` html     > html_list.html

mv ./reports ..
