#!/bin/sh
#
# Copyright (C) 2017-2018, 2020  Etersoft
# Copyright (C) 2017-2018, 2020  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

load_helper epm-assure
load_helper epm-repack-rpm


__epm_create_fake_help()
{
    message 'epm create-fake - create package with fake provides and requires
Usage: epm create-fake [options] <package-name>
       epm create-fake [options] <file.yaml>'
    echo ''
    echog 'Options:'
    get_help HELPOPT $SHAREDIR/epm-create_fake
    message '
Examples:
    # epm create-fake --install python-somepackage
    # epm create-fake --install --provides="python3dist(somepackage)" python-somepackage
    # epm create-fake --install --requires=python3 --requires=python3-module python-somepackage
    # epm create-fake --name=libfoo --provides="libfoo.so.1()(64bit)" libfoo
    # epm create-fake --install mypackage.yaml
'
}

epm_create_fake()
{
    local VERSION=0
    local RELEASE=0
    local REQUIRES=""
    local PROVIDES=""
    local FAKENAME=""
    local NAME=""

    for i in "$@"; do
        case $i in
            -h|--help)          # HELPOPT: show this help
                __epm_create_fake_help
                return
                ;;
            --install)          # HELPOPT: auto install fake package
                ;;
            --name=*)           # HELPOPT: set exact package name (without fake- prefix)
                FAKENAME="${i#*=}"
                ;;
            --version=*)        # HELPOPT: set package version (default: 0)
                VERSION="${i#*=}"
                ;;
            --release=*)        # HELPOPT: set package release (default: 0)
                RELEASE="${i#*=}"
                ;;
            --requires=*)       # HELPOPT: set package requires (repeat for multiple)
                REQUIRES="$REQUIRES ${i#*=}"
                ;;
            --provides=*)       # HELPOPT: set package provides (repeat for multiple)
                PROVIDES="$PROVIDES ${i#*=}"
                ;;
            *)
                [ -z "$NAME" ] && NAME="$i"
                ;;
        esac
    done

    if [ -z "$NAME" ] ; then
        fatal 'Error: You have to specify PACKAGE_NAME or a yaml file'
    fi

    # will set RPMBUILD
    __assure_exists_rpmbuild

    HOME="$(mktemp -d --tmpdir=$BIGTMPDIR)" || fatal
    unset BASH_ENV
    remove_on_exit $HOME
    export HOME
    __create_rpmmacros

    local tmpbuilddir=$HOME/create-fake.tmpdir
    mkdir $tmpbuilddir

    local yamlfile=""
    local pkgname=""

    # handle yaml file input
    if echo "$NAME" | grep -q '\.yaml$' ; then
        yamlfile="$(realpath "$NAME")"
        [ -r "$yamlfile" ] || fatal 'Can'\''t read yaml file: $NAME'
        # read name and version from yaml
        local name="" version=""
        yaml_load_vars "$yamlfile" name version
        pkgname="$name"
        VERSION="$version"
    else
        # generate yaml from CLI args
        if [ -n "$FAKENAME" ] ; then
            pkgname="$FAKENAME"
        else
            pkgname="fake-$NAME"
            # auto-add original name to provides (like the old Provides: %{fake_name})
            PROVIDES="$NAME $PROVIDES"
        fi

        yamlfile="$tmpbuilddir/eepm.yaml"
        cat > "$yamlfile" <<EOF
name: $pkgname
version: $VERSION
release: $RELEASE
summary: Faked provides package
license: CC0
group: Other
buildarch: noarch
description: This package is empty. It has been created to put fake entry in rpmdb.
EOF
        # echo without quotes trims and normalizes whitespace
        [ -n "$(echo $REQUIRES)" ] && echo "requires: $(echo $REQUIRES)" >> "$yamlfile"
        [ -n "$(echo $PROVIDES)" ] && echo "provides: $(echo $PROVIDES)" >> "$yamlfile"
    fi

    info 'Generated YAML for fake package:'
    cat "$yamlfile"

    cd $tmpbuilddir/ || fatal

    __generate_spec_by_yaml "$yamlfile" "$tmpbuilddir"

    local buildroot="$tmpbuilddir/$pkgname-$VERSION"
    local spec="$tmpbuilddir/$pkgname.spec"
    mkdir -p "$buildroot"

    showcmd $RPMBUILD --buildroot $buildroot -bb $spec
    if [ -n "$verbose" ] ; then
        a='' $RPMBUILD --buildroot $buildroot -bb $spec || fatal
    else
        a='' $RPMBUILD --buildroot $buildroot -bb $spec >/dev/null || fatal
    fi

    local repacked_rpm="$(realpath $tmpbuilddir/../*.rpm)"
    remove_on_exit "$repacked_rpm"

    if [ -n "$install" ] ; then
        epm install "$repacked_rpm"
    else
        cp $verbose "$repacked_rpm" "$EPMCURDIR/" || fatal
        local rpmname
        rpmname="$(basename "$repacked_rpm")"
        info 'Fake package saved: $rpmname'
    fi
}
