#!/bin/sh
#
# Copyright (C) 2015,2016  Etersoft
# Copyright (C) 2015,2016  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-query
load_helper epm-print

PAOURL="https://packages.altlinux.org"

paoapi()
{
    # http://petstore.swagger.io/?url=http://packages.altlinux.org/api/docs
    assure_exists curl || return 1
    showcmd curl "$PAOURL/api/$1"
    a='' curl -s --header "Accept: application/json" "$PAOURL/api/$1"
}

# TODO: use /home/lav/Projects/git/JSON.sh
get_pao_var()
{
    local FIELD="$1"
    #grep '"$FIELD"' | sed -e 's|.*"$FIELD":"||g' | sed -e 's|".*||g'
    $SHAREDIR/tools_json -b | grep -E "\[.*\"$FIELD\"\]" | sed -e 's|.*[[:space:]]"\(.*\)"|\1|g'
    return 0
}


run_command_if_exists()
{
    local CMD="$1"
    shift
    if is_command "$CMD" ; then
        docmd $CMD "$@"
        return 0
    fi
    return 1
}

# TODO: use something like xdg-browser
open_browser()
{
    local i
    for i in xdg-open firefox chromium links ; do
        run_command_if_exists $i "$@" && return
    done
}

__query_package_hl_url()
{
    case $DISTRNAME in
        ALTLinux|ALTServer)
            paoapi srpms/$1 | get_pao_var url
            ;;
    esac
    return 1
}

query_package_url()
{
    local URL

    case $PMTYPE in
        *-rpm)
            # TODO: for binary packages?
            query_package_field URL "$1" || __query_package_hl_url "$1"
            #LANG=C epm info "$1"
            return
            ;;
        homebrew)
            docmd brew "$1" | grep "^From: " | sed -e "s|^From: ||"
            return
            ;;
    esac
    fatal "rpm based distro supported only. TODO: Realize via web service?"
}

get_locale()
{
    local loc
    loc=$(a='' natspec --locale 2>/dev/null)
    [ -n "$loc" ] || loc=$LANG
    echo $loc
}

get_pao_url()
{
    local loc
    loc=$(get_locale | cut -c1-2)
    case $loc in
        en|ru|uk|br)
            loc=$loc
            ;;
        *)
            loc=en
    esac
    echo "$PAOURL/$loc/Sisyphus/srpms"
}

query_altlinux_url()
{
    local URL
    case $PMTYPE in
        *-rpm)
            local srpm=$(print_srcname "$1")
            [ -n "$srpm" ] || fatal "Can't get source name for $1"
            echo "$(get_pao_url)/$srpm"
            return
            ;;
    esac
    fatal "rpm based distro supported only. TODO: Realize via web service?"
}

epm_site()
{

[ -n "$pkg_filenames" ] || fatal "Info: package name is missed"

local PAO=""
for f in $pkg_names $pkg_files ; do
    [ "$f" = "-p" ] && PAO="$f" && continue
    if [ -n "$PAO" ] ; then
        pkg_url=$(query_altlinux_url $f)
    else
        pkg_url=$(query_package_url $f)
    fi
    [ -n "$pkg_url" ] && open_browser "$pkg_url" && continue
    warning "Can't get URL for $f package"
done

#for f in $pkg_names ; do
#    LANG=C epm info $f
#done

# TODO: -p for p.a.o (see rpmurl)

}