#!/usr/bin/bash

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

# webext 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.

function IdentifyWebextId {
    # Called by WebextRulesDH WebextInstall

    if [ -z ${webextID} ] && [ -f ${GitPath}/manifest.json ]
    then
        webextID=$(grep '"id":' ${GitPath}/manifest.json)
        webextID=$(echo ${webextID} | sed --expression='s/^\"id\": \"//')
        webextID=$(echo ${webextID} | sed --expression='s/\",\s*//')

        echo -e "Notice from Webext-Plugin: WebextID = "${webextID} >> ${log}
    fi

    if [ -z ${webextID} ]
    then
        webextID="PLEASE_REPLACE_WITH_ID"
        whiptail --title "ID not found" \
        --msgbox "Didn't find ID or manifest.json in ${GitPath}" 15 60
    fi
}

function WebextRules {
    # Called by DebianRulesTemplate

    # These strings will be added to str4rules

    Package=$(echo ${SourceName} | tr "a-z" "A-Z")
    echo -e ${Package}"_FILES =  \\" >> ${GitPath}/debian/rules

    PackageL=$(ls ${GitPath})
    PackageA=(${PackageL})

    for element in ${PackageA[*]}
    do
        if [ "${element}" != "debian" ]
        then
            echo -e ${element}" \\" >> ${GitPath}/debian/rules
        fi
    done
    echo "\$(NULL)" >> ${GitPath}/debian/rules

    echo -e "\n Uncomment the following lines \n"
    echo "if there are files to exclude and add them."
    echo -e "\n# "${Package}"_FILES_EXCLUDE =  \\" \
    >> ${GitPath}/debian/rules
    echo -e "# \$(NULL)\n" >> ${GitPath}/debian/rules
}

function WebextRulesDH {
    # Called by DebianRulesTemplate

    IdentifyWebextId

    DHCleanStr="override_dh_clean:\n\tdh_clean\n\trm -rf debian/build\n"

    DHAutoBuildIndepStr1="override_dh_auto_build-indep:"
    DHAutoBuildIndepStr2="\tmkdir \$(CURDIR)/debian/build && \\"
    Str3B="\tzip --recurse-paths "
    DHAutoBuildIndepStr3=${Str3B}"\$(CURDIR)/debian/build/"${webextID}".xpi \\"
    DHAutoBuildIndepStr4="\t    \$(${Package}_FILES) \\"
    DHAutoBuildIndepStr5="# \t    --exclude \$(${Package}_FILES_EXCLUDE)"
    DHAutoBuildIndepStr6="\tdh_auto_build\n"

    echo -e ${DHCleanStr} >> ${GitPath}/debian/rules
    echo -e ${DHAutoBuildIndepStr1} >> ${GitPath}/debian/rules
    echo -e ${DHAutoBuildIndepStr2} >> ${GitPath}/debian/rules
    echo -e ${DHAutoBuildIndepStr3} >> ${GitPath}/debian/rules
    echo -e "${DHAutoBuildIndepStr4}" >> ${GitPath}/debian/rules
    echo -e "${DHAutoBuildIndepStr5}" >> ${GitPath}/debian/rules
    echo -e ${DHAutoBuildIndepStr6} >> ${GitPath}/debian/rules
}

function WebextControl {
    # Called by DebianControlTemplate
    # TB specific, for FF 'web'
    sed --in-place \
    --expression="s/Section: /Section: mail/" ${GitPath}/debian/control

    # 'a' means append. The string after the 'a' will be appended 
    # to the sting before the 'a'.
    # @X escapes the space at the beginning of the appended line.
    # It will be removed later.
    sed --in-place \
    --expression="/Build-Depends: debhelper-compat ${str4versiondebhelpers}/ \
    a @X , zip" \
    ${GitPath}/debian/control
    # TB specific, for FF 'firefox-esr (>= 91.4)'
    sed --in-place \
    --expression="/Depends: \${misc:Depends}/ \
    a @X , thunderbird (>= 1:128.3)" \
    ${GitPath}/debian/control
    sed --in-place --expression="s/^@X//g" ${GitPath}/debian/control
}

function WebextInstall {
    # Called by DisplayDebianFiles
    IdentifyWebextId
    InstallStr="debian/build/"${webextID}".xpi\tusr/share/webext"
    echo -e ${InstallStr} >> ${GitPath}/debian/${PackName}.install
}

function WebextDocs {
    # Called by
    echo "Still empty"
}

function WebextLinksTB {
    # Called by DisplayDebianFiles
    IdentifyWebextId
    SourceStr="/usr/share/webext/"${webextID}".xpi\t"
    TargetStr="/usr/lib/thunderbird/extensions/"${webextID}".xpi"
    echo -e ${SourceStr}${TargetStr} >> \
    ${GitPath}/debian/${PackName}.links
}

whiptail --title "webext plugin found" \
--msgbox "build-gbp-webext-plugin.sh was loaded." 15 60
# This is the end, my friend
#generated on Sun, 04 May 2025 09:23:46 +0000
