#!/bin/sh
#
# Copyright (c) 2016-2026 Linagora
# 
# This program/library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# This program/library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program/library; If not, see http://www.gnu.org/licenses/
# for the GNU Lesser General Public License version 2.1.
#
#-----------------------------------------------------------------------------
# Usage:
#     > checkCopyrightStartDate.sh <SVN or GIT Url of the root dir to check>
#
#
# 1st - Check the project copyright start date. The copyright start date of
#       the project is the older date of the PROJECT SOURCE CODE overall SCM
#       (Petalslink SVN or OW2 SVN)
# 2nd - Check the POM file copyright start date. This copyright date is the
#       older date of the PROJECT overall SCM (Petalslink SVN or OW2 SVN)
#
# Note: The project copyright start date and the POM file copyright start
#       date can be different when the project embeds source code comming
#       from another existing project that was reworked.
#

checkOnePom() {

   COPYRIGHT_FILE=$1
   PROJECT_DIR=`dirname "${COPYRIGHT_FILE}"`
   PROJECT_DIR_REL=`echo "${PROJECT_DIR}" | sed -e "s%${WORKING_AREA}/%%"`
   POM_FILE=`echo ${PROJECT_DIR}/pom.xml`

   if [ -f "$POM_FILE" ]
   then
      FIRST_COMMIT=`git rev-list HEAD "${PROJECT_DIR_REL}/pom.xml" | tail -n 1`
      GIT_START_DATE=`git log ${FIRST_COMMIT} "${PROJECT_DIR_REL}/pom.xml" | grep -E "Date:.*[0-9]{4}" | sed -e "s/.*[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\).*/\1/"`
      COPYRIGHT_START_DATE=`cat "${POM_FILE}" | grep -E "^ Copyright.*$" | sed -e "s/^ Copyright (c) \([0-9]\{4\}\).*/\1/"`
      if [ "${GIT_START_DATE}" != "${COPYRIGHT_START_DATE}" ]
      then
         REWORKED_DATE=`cat "${SCRIPT_PATH}/history-references" | grep "${SCM_URL}/${PROJECT_DIR_REL}/pom.xml=" | cut -d'=' -f2`
         if [ -n "${REWORKED_DATE}" ]
         then
            if [ "${REWORKED_DATE}" != "${COPYRIGHT_START_DATE}" ]
            then
               echo "   Copyright start date not aligned with reworked date. Expected copyright start date for POM file: ${REWORKED_DATE}"
               return 1
            else
               #echo "   Copyright start date aligned for project ${PROJECT_DIR} with reworked date"
               return 0
            fi
         else
            echo "   Copyright start date not aligned with Petalslink Git content (no reworked date URL mapping defined for this module). Expected copyright start date for POM file: ${GIT_START_DATE}"
	    return 1
         fi
      else
         #echo "   Copyright start date aligned for project ${PROJECT_DIR} with Petalslink GIT"
         return 0
      fi
   fi
}

checkOneProject() {

   COPYRIGHT_FILE=$1
   PROJECT_DIR=`dirname "${COPYRIGHT_FILE}"`
   PROJECT_DIR_REL=`echo "${PROJECT_DIR}" | sed -e "s%${WORKING_AREA}/%%"`

   FIRST_COMMIT=`git rev-list HEAD "${PROJECT_DIR_REL}" | tail -n 1`
   GIT_START_DATE=`git log ${FIRST_COMMIT} "${PROJECT_DIR_REL}" | grep -E "Date:.*[0-9]{4}" | sed -e "s/.*[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\).*/\1/"`
   COPYRIGHT_START_DATE=`cat "${PROJECT_DIR}/COPYRIGHT" | grep -E "^ Copyright.*$" | sed -e "s/^ Copyright (c) \([0-9]\{4\}\).*/\1/"`
   if [ "${GIT_START_DATE}" != "${COPYRIGHT_START_DATE}" ]
   then
      REWORKED_DATE=`cat "${SCRIPT_PATH}/history-references" | grep "${SCM_URL}/${PROJECT_DIR_REL}=" | cut -d'=' -f2`
      if [ -n "${REWORKED_DATE}" ]
      then
         if [ "${REWORKED_DATE}" != "${COPYRIGHT_START_DATE}" ]
         then
            echo "   Copyright start date not aligned with reworked date. Expected copyright start date for POM file: ${REWORKED_DATE}"
            return 1
         else
            #echo "   Copyright start date aligned for project ${PROJECT_DIR} with reworked date"
            return 0
         fi
      else
         echo "   Copyright start date not aligned with Petalslink Git content (no reworked date URL mapping defined for this module). Expected copyright start date for POM file: ${GIT_START_DATE}"
	 return 1
      fi
   else
      #echo "   Copyright start date aligned for project ${PROJECT_DIR} with Petalslink Git"
      return 0
   fi
}

checkRepository() {
   FAIL="false"
   for COPYRIGHT_FILE in `find $1 -name COPYRIGHT | grep -v target`
   do
      echo "Processing project associated to ${COPYRIGHT_FILE}"
      checkOneProject ${COPYRIGHT_FILE}
      if [ $? -ne 0 ]
      then
         FAIL="true"
      fi

      echo "Processing associated POM file of ${COPYRIGHT_FILE}"
      checkOnePom ${COPYRIGHT_FILE}
      if [ $? -ne 0 ]
      then
         FAIL="true"
      fi
   done

   if [ "${FAIL}" = "true" ]
   then
      echo "At least one copyright start date is not aligned !"
      return 1
   else
      return 0
   fi
}

SCM_URL=`echo $1`
SCRIPT_PATH=`dirname "$(readlink -f $0)"`
RANDOM=`tr -cd 0-9 </dev/urandom | head -c 4`
WORKING_AREA="/tmp/git-$$-${RANDOM}"
git clone ${SCM_URL} ${WORKING_AREA} && \
cd ${WORKING_AREA}
checkRepository ${WORKING_AREA}
RET=`echo $?`
rm -rf ${WORKING_AREA}
exit ${RET}

