#!/bin/sh -e


###############################################################################
# JAMI PACKAGE POSTINST                                                       #
#                                                                             #
# Install Jami's package repository to the trusted sources and add Jami's     #
# release key to trusted keyring.                                             #
#                                                                             #
###############################################################################


###############################################################################
# [1] Configuration                                                           #
###############################################################################

JAMI_REPO_BASE="https://dl.jami.net/nightly"

# Jami release key.
JAMI_KEY_FINGERPRINT="A295D773307D25A33AE72F2F64CD5FA175348F84"
JAMI_KEY="\
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2

mQENBFVSdlcBCAC9zC1rp12O2K08PGozI14Y+t4qC931eHicvkuEMF1B9gAhjdRF
aIJS+UXwgQzoamDIHenxz1Q3fXUjKCMXytjGymB/0LUKccSbtH0Rcsl8kZ2z57KN
E+GLS7SvlP93ZOxco7eAEBWF/fvMrCsm10sNI6bW7UK0bgql9iIetd6Wrp9xXFVs
gmoV8Av714OlswsthSNtN+xQls3ozQ/dVGsOkZEyDbBzi88/rQEtuIDztTSWyD0V
x7WaY5+mVRwsJKzyPlgvsXpbP7A41IFykeOzPKh+vYz3k7dcLIRdOwse79oT2RXt
2VYEyTyTZIQlCJjGNTJYsU7GVffU4LnI7p/bABEBAAG0QFJpbmcgLSBTYXZvaXIt
RmFpcmUgTGludXgsIEluYyA8cmluZ0BsaXN0cy5zYXZvaXJmYWlyZWxpbnV4Lm5l
dD6JATkEEwEIACMFAlVSdlcCGwMHCwkIBwMCAQYVCAIJCgsEFgIDAQIeAQIXgAAK
CRBkzV+hdTSPhMqSB/9aaKXVByoE7zwPM8DDSntS/jYhhaV1CcZ78WeC1LY2UnHL
R/yvABnDMikgqbMHBDu1R9dnjgZNntp7p0blxcT6ScxdZ6FpMZG6ZW5YNZIVctnF
jdExStcdpcbbycW8GeCmQdVcKLBl8G55mick02ayVNOH5ETtWahPwBvMWk5hSaH3
E18yvnmdqa9nCEST4UKvBOpmn58mwJDIR2YLdEFcL8z4FkxQUNkx/hiHSn+YLorZ
H3iBrf9y9vSYhQ5pxx7seWkoaG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hi1TssiWPOU0hQgn4BIreYnEZUA6xuFX10C7k2DVRFZrXF7lpNgPQ8eNZTnQBIuw
HUFCGSHO3/kzxSlkE1PBUX3IZ8PSFijyopBnWUhlSXuyRjte8OR7Fl/Rlf0IaOD1
4sRdAfS333T4Uifq4uOu
=s2aQ
-----END PGP PUBLIC KEY BLOCK-----"

# System paths and generated variables
# In general: you will not need to modify these variables.
# WARNING: When modifying the paths, keep in mind that the corresponding
# postrm paths should be modified as well
REPO_FILE="/etc/yum.repos.d/jami-main.repo"
GPG_FILE="/etc/pki/rpm-gpg/RPM-GPG-KEY-JAMI"

is_distribution_supported() {

    # Detect currently running system using /etc/os-release
    # Redhat-based systems are supposed to provide /etc/os-release so
    # if it's not the case then we simply don't want to provide
    # automatic updates.
    if ! [ -f /etc/os-release ]; then
        return 1
    fi

    # This defines variables such as NAME, ID, VERSION_ID, etc.
    . /etc/os-release

    case ${ID}_${VERSION_ID%.*} in
        rhel_8|fedora_32|fedora_33|fedora_34|opensuse-leap_15.2|opensuse-tumbleweed)
            return 0
    esac

    return 1
}

###############################################################################
# [3] Maintainer script main switch                                           #
###############################################################################

if is_distribution_supported; then
    CAN_ADD_REPO_SOURCE=true
else
    CAN_ADD_REPO_SOURCE=false
fi

if command -v rpm > /dev/null; then
    # RPM is required to add the key.
    CAN_ADD_REPO_SOURCE=false
fi

JAMI_REPO='
[jami]
name='"${NAME}"' $releasever - $basearch - Jami
baseurl='"${JAMI_REPO_BASE}"/"${ID}"'_$releasever
gpgcheck=1
gpgkey=https://dl.jami.net/jami.pub.key
enabled=1'

# Set-up Jami repository end tag
if [ "${PLATFORM_ID}" = "opensuse-leap:15.2" ]; then
    ENDTAG="opensuse-leap_15.2"
    REPO_FILE="/etc/zypp/repos.d/jami-main.repo"
    GPG_FILE="/tmp/RPM-GPG-KEY-JAMI"
elif [ "${PLATFORM_ID}" = "opensuse-tumbleweed" ]; then
    ENDTAG="opensuse-tumbleweed"
    REPO_FILE="/etc/zypp/repos.d/jami-main.repo"
    GPG_FILE="/tmp/RPM-GPG-KEY-JAMI"
    # Remove releasever for tumbleweed as it's a rolling release
    JAMI_REPO='
[jami]
name='"${NAME}"' $basearch - Jami
baseurl='"${JAMI_REPO_BASE}"/"${ID}"'
gpgcheck=1
gpgkey=https://dl.jami.net/jami.pub.key
enabled=1'
fi

if [ "${CAN_ADD_REPO_SOURCE}" = "true" ]; then
    # Add the key to the trusted keyring.
    echo "$JAMI_KEY" > "$GPG_FILE"

    rm -f /var/lib/rpm/.rpm.lock > /dev/null 2>&1
    rpm --import "$GPG_FILE"  > /dev/null 2>&1

    # Add an entry for the package repository to the trusted package.
    # XXX: The NAME and ID variables are set as a side-effect of
    # sourcing the /etc/os-release file in the above
    # is_distribution_supported call.
    cat > $REPO_FILE <<EOF
$JAMI_REPO
EOF
fi
