#!/usr/bin/bash

# Copyright 2019-2025 Mechtilde and Michael Stehmann <mechtilde@debian.org>
# version 0.9.9

# maven plugin for build-gbp.sh

# 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 3 of the License, or
# (at your option) any later version.

# This program 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 31 Milk Street, #960789 Boston,
# MA 02196, USA.

# Dependencies: maven-debian-helper licensecheck apt-file

function Rules4MavenDH {
    # Called by DebianRulesTemplates

    sed --in-place \
    --expression="s/dh \$@/dh \$@ --buildsystem=maven/" \
    ${GitPath}/debian/rules

}
function ShowMaven {
    # Called by DisplayDebianFiles MakeMaven

    mavenl=$(ls ${GitPath}/debian/ | grep 'maven')
    for element in ${mavenl[*]}
    do
        nano --linenumbers --mouse --softwrap debian/${element}
    done
    
    pomsl=$(ls ${GitPath}/debian/ | grep ${PackName})
    for element in ${pomsl[*]}
    do
        nano --linenumbers --mouse --softwrap debian/${element}
    done
}

function AskChrootPath {
    # Called by IdentifyMavenChrootPath
    
    # This is the way from GitPath to /srv/maven-chroot
    ChrootPath=$(whiptail --title "Maven chroot path" \
    --inputbox "Please insert the path\nto the maven chroot directory\n\
    on the host:" \
    --nocancel 15 60 3>&2 2>&1 1>&3)

    if [ -n "${OldCP}" ]
    then
        ReplaceConfigLines "ChrootPath" ${ChrootPath}
    else
        echo "### Maven chroot path"
        echo "ChrootPath="${ChrootPath} >> ${ConfigPath}${OrigName}
    fi
    OldCP=""
}

function AskWorkspacePath {
    # Called by IdentifyMavenChrootPath

    # This is the way to /home/user/
    Path2Workspace=$(whiptail --title "Maven chroot path" \
    --inputbox "Please insert the path\nto the workspace directory:" \
    --nocancel 15 60 3>&2 2>&1 1>&3)

    if [ -n "${OldP2W}" ]
    then
        ReplaceConfigLines "Path2Workspace" ${Path2Workspace}
    else
        echo "Path2Workspace="${Path2Workspace} >> ${ConfigPath}${OrigName}
    fi
    OldP2W=""
}

function IdentifyMavenChrootPath {
    # Called by MakeMaven and itself
    
    if [ -n "${ChrootPath}" ]
    then
        if ! whiptail --title "Maven chroot path " \
        --yesno "Is ${ChrootPath}\nthe right path to\n\
        maven chroot directory on the host?" \
        --yes-button "Yes" \
        --no-button "No" 15 60
        then
            OldCP=${ChrootPath}
            AskChrootPath
        fi
    else
        AskChrootPath
    fi

    # Create Maven-Chroot if not exists
    if ! [ -d ${ChrootPath} ]
    then
        echo "Please enter your SUDO password!"
        sudo mkdir --parents ${ChrootPath}
    fi
    
    if ! [ -d ${ChrootPath}/usr ]
    then
        echo "Please enter your SUDO password!"
        sudo /usr/sbin/debootstrap --arch amd64 sid \
        ${ChrootPath} http://ftp.de.debian.org/debian
        echo "The maven chroot has been created." >> ${log}
    fi

    # The workspace is /home/user
    if [ -n "${Path2Workspace}" ]
    then
        if ! whiptail --title "Maven chroot path " \
        --yesno "Is ${Path2Workspace}\nthe workspace in the maven chroot?" \
        --yes-button "Yes" \
        --no-button "No" 15 60 # test
        then
            OldP2W=${Path2Workspace}
            AskWorkspacePath
        fi
    else
        AskWorkspacePath
    fi
    # Replace / at the end
    Path2Workspace=$(echo ${Path2Workspace} | sed --expression="s/\/$//")
    MavenChrootPath=${ChrootPath}${Path2Workspace}
    if ! [ -d ${MavenChrootPath} ]
    then
        whiptail --title "Error!" \
        --msgbox "${MavenChrootPath} does not exist.\n\
        It will be created now." 15 60
        echo "Please enter your SUDO password!"
        sudo mkdir --parents ${MavenChrootPath}
        echo "The maven work space has been created." >> ${log}
    fi
}

function MakeMaven {
    # Called by BuildNewRevision

    cd ${GitPath}
    IdentifyMavenChrootPath

    # Copy workspace to maven chroot
    echo "To copy the sources into maven-chroot you need sudo rights"
    sudo cp --archive ${GitPath} ${MavenChrootPath}

    # Because mh_make has to run in the directory, where pom.xml is
    echo "pom.xml is here:"
    find . -name 'pom.xml' -print
    echo "Please switch to the Maven chroot in another terminal."
    echo
    echo "Use these commands:"
    echo
    echo "# sudo mount -o bind /proc "${ChrootPath}"/proc"
    echo "# sudo mount devpts /dev/pts -t devpts"
    echo
    echo "sudo LANG=C chroot "${ChrootPath}" /usr/bin/bash"
    echo "cd "${Path2Workspace}"/"${SourceName}
    echo "try: apt install maven-debian-helper"
    echo "apt update && apt upgrade"
    echo "mh_make --verbose "${SourceName}
    echo
    echo "Then follow the questions of mh_make"
    echo
    echo "You can leave the chroot with 'exit'."
    echo
    echo "After finishing press return to go on!"
    read a

    #--run-tests=true --javadoc=true --verbose
    #mh_make --package=${SourceName} --bin-package=${PackName} \
    #--run-tests=false --javadoc=false --verbose
    #if [ $? -ne 0 ]
    #then
    #    echo "mh_make failed!"
    #    exit
    #else    
    #    echo "mh_make has been executed for "${PackName} >> ${log}
    #fi
    
    cp --recursive --update ${MavenChrootPath}/${SourceName}/debian \
    ${GitPath}    

    if [ -w debian/maven.rules ]
    then
        echo "#[groupId] [artifactId] [type] [version] [classifier] [scope]" \
        >> debian/maven.rules
    fi

    ShowMaven


    # Patch for mh_make bug
    str4standardsversion="4.7.1"
    cd ${GitPath}/debian/
    less --LINE-NUMBERS control

    sed --in-place --expression=\
    "s/Standards-Version: 4.4.1/Standards-Version: ${str4standardsversion}/" \
    control
    # 'a' means append. The string after the 'a' will be appended 
    # to the sting before the 'a'.
    sed --in-place \
    --expression="/Standards-Version: ${str4standardsversion}/ \
    a Rules-Requires-Root: no" \
    control
    sed --in-place --expression=\
    "s/debhelper-compat (=12)/debhelper-compat ${str4versiondebhelpers}/"  \
    control

    cd ${GitPath}
    whiptail --title "Check debian/control" \
    --msgbox "Please check the control file another time!" 15 60
    less --LINE-NUMBERS ${GitPath}/debian/control
}

whiptail --title "maven plugin found" \
--msgbox "build-gbp-maven-plugin.sh was loaded." 15 60

echo "build-gbp-maven-plugin.sh was loaded." >> ${log}
# Next the function MakeMaven is executed by the main script.
# This is the end, my friend
#generated on Sun, 04 May 2025 09:23:46 +0000
