#!/bin/sh
#------------------------------------------------------------------------------
# /var/install/bin/subversion-commit-mail - send e-mail notification
#
# Creation   :  2018-07-11 daniel
# Last Update:  2023-07-23 09:11:10
#
# Copyright (c) 2025 the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#------------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# initialize global values
#-----------------------------------------------------------------------------
REPO="$1"
REV="$2"
MAILRECEIVER="$3"
REPONAME="$4"

#----------------------------------------------------------------------------
# check user name and user's real name
#----------------------------------------------------------------------------
UNIXUSER=$(whoami)
REALUSER=$(cat /etc/passwd | grep "^$UNIXUSER:" | cut -d: -f5)

if [ "${UNIXUSER}" = "wwwrun" ] || [ "${UNIXUSER}" = "root" ]
then
    REALUSER="SVN Commit"
fi

#----------------------------------------------------------------------------
# email data
#----------------------------------------------------------------------------
{
    echo "Date: $(date "+%a, %e %b %Y %X %z")"
    echo "From: ${REALUSER} <${UNIXUSER}@$(hostname -f)>"
    echo "To: Commit Info <${MAILRECEIVER}>"
    echo "Subject: ${REPONAME} SVN commit report [${REV}]"
    echo "Content-Type: text/plain; charset=UTF-8"
    echo "Content-Transfer-Encoding: 8bit"
    echo ""
    echo "Revision: ${REV}"
    echo ""
    echo "Author:   $(svnlook author ${REPO} -r ${REV})"
    echo "Date:     $(svnlook date ${REPO} -r ${REV})"
    echo ""
    echo "Log Message:"
    echo "-----------"
    echo "$(svnlook log ${REPO} -r ${REV})"
    echo ""

    svnlook changed ${REPO} -r ${REV} | \
    {
        while read line
        do
            case "${line:0:1}" in
            A)
                changes_added="${changes_added} ${line:4}"
                ;;
            D)
                changes_deleted="${changes_deleted} ${line:4}"
                ;;
            U)
                changes_modified="${changes_modified} ${line:4}"
                ;;
            esac

            if [ "${line:1:1}" != " " ]
            then
                changes_properties="${changes_propertes} ${line:4}"
            fi
        done

        if [ "${changes_modified}" != "" ]
        then
            echo "Modified Paths:"
            echo "--------------"
            for change in ${changes_modified}
            do
                echo "   $change"
            done
            echo ""
        fi

        if [ "${changes_added}" != "" ]
        then
            echo "Added Paths:"
            echo "-----------"
            for change in ${changes_added}
            do
                echo "   $change"
            done
            echo ""
        fi

        if [ "${changes_deleted}" != "" ]
        then
            echo "Deleted Paths:"
            echo "-------------"
            for change in ${changes_deleted}
            do
                echo "   $change"
            done
            echo ""
        fi

        if [ "${changes_properties}" != "" ]
        then
            echo "Property Changed:"
            echo "----------------"
            for change in ${changes_properties}
            do
                echo "   $change"
            done
            echo ""
        fi
    }
    echo "."
} | /usr/sbin/sendmail "${MAILRECEIVER}"