#!/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}"`
   POM_FILE=`echo ${PROJECT_DIR}/pom.xml`

   if [ -f $POM_FILE ]
   then
      SVN_START_DATE=`svn --non-interactive --username anonymous --password anonymous log --revision 1:HEAD --limit 1 --quiet "${POM_FILE}"  | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2}" | sed -e "s/.*| \([0-9]\{4\}\)-[0-9]\{2\}-[0-9]\{2\}.*/\1/"`
      COPYRIGHT_START_DATE=`cat "${POM_FILE}" | grep -E "^ Copyright.*$" | sed -e "s/^ Copyright (c) \([0-9]\{4\}\).*/\1/"`
      if [ "${SVN_START_DATE}" != "${COPYRIGHT_START_DATE}" ]
      then
         SVN_PETALSLINK_URL=`svn --non-interactive --username anonymous --password anonymous info "${POM_FILE}" | grep -E "^URL" | sed -e "s/^URL.\{0,1\}: \(http.*\)$/\1/"`
         OW2_FORGE_DATE=`cat "${SCRIPT_PATH}/history-references" | grep "${SVN_PETALSLINK_URL}=" | cut -d'=' -f2`
         if [ -n "${OW2_FORGE_DATE}" ]
         then
            if [ "${OW2_FORGE_DATE}" != "${COPYRIGHT_START_DATE}" ]
            then
               echo "   Copyright start date not aligned with OW2 SCM content. Expected copyright start date for POM file: ${OW2_FORGE_DATE}"
               return 1
            else
               #echo "   Copyright start date aligned for project ${PROJECT_DIR} with OW2 SVN"
               return 0
            fi
         else
            echo "   Copyright start date not aligned with Petalslink SCM content (no OW2 SCM URL mapping defined for this module). Expected copyright start date for POM file: ${SVN_START_DATE}"
            return 1
         fi
      else
         #echo "   Copyright start date aligned for project ${PROJECT_DIR} with Petalslink SVN"
         return 0
      fi
   fi
}

checkOneProject() {

   COPYRIGHT_FILE=$1
   PROJECT_DIR=`dirname "${COPYRIGHT_FILE}"`

   SVN_START_DATE=`svn --non-interactive --username anonymous --password anonymous log --revision 1:HEAD --limit 1 --quiet "${PROJECT_DIR}"  | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2}" | sed -e "s/.*| \([0-9]\{4\}\)-[0-9]\{2\}-[0-9]\{2\}.*/\1/"`
   COPYRIGHT_START_DATE=`cat "${PROJECT_DIR}/COPYRIGHT" | grep -E "^ Copyright.*$" | sed -e "s/^ Copyright (c) \([0-9]\{4\}\).*/\1/"`
   if [ "${SVN_START_DATE}" != "${COPYRIGHT_START_DATE}" ]
   then
      SVN_PETALSLINK_URL=`svn --non-interactive --username anonymous --password anonymous info "${PROJECT_DIR}" | grep -E "^URL" | sed -e "s/^URL.\{0,1\}: \(http.*\)$/\1/"`
      OW2_FORGE_DATE=`cat "${SCRIPT_PATH}/history-references" | grep "${SVN_PETALSLINK_URL}=" | cut -d'=' -f2`
      if [ -n "${OW2_FORGE_DATE}" ]
      then
         if [ "${OW2_FORGE_DATE}" != "${COPYRIGHT_START_DATE}" ]
         then
            echo "   Copyright start date not aligned with OW2 SCM content. Expected copyright start date for project: ${OW2_FORGE_DATE}"
            return 1
         else
            #echo "   Copyright start date aligned for project ${PROJECT_DIR} with OW2 SVN"
            return 0
         fi
      else
         echo "   Copyright start date not aligned with Petalslink SCM content (no OW2 SCM URL mapping defined for this module). Expected copyright start date for project: ${SVN_START_DATE}"
         return 1
      fi
   else
      #echo "   Copyright start date aligned for project ${PROJECT_DIR} with Petalslink SVN"
      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
}

SCRIPT_PATH=`dirname "$(readlink -f $0)"`
RANDOM=`tr -cd 0-9 </dev/urandom | head -c 4`
WORKING_AREA="/tmp/svn-$$-${RANDOM}"
svn --non-interactive --username anonymous --password anonymous co $1 ${WORKING_AREA} && \
checkRepository ${WORKING_AREA}
RET=`echo $?`
rm -rf ${WORKING_AREA}
exit ${RET}

