#!/bin/sh
#
# Copyright (C) 2014, 2017  Etersoft
# Copyright (C) 2014, 2017  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/>.
#

# apt-file like. See also
# https://bugzilla.altlinux.org/show_bug.cgi?id=14449

# convert "http://download.etersoft.ru/pub/ALTLinux/p8/branch x86_64" to /tmp/epm/ALTLinux/p8/branch/x86_64
get_local_alt_mirror_path()
{
    local DN1=$(dirname "$1")
    local DN2=$(dirname $DN1)
    local DN3=$(dirname $DN2)

    local BN0=$(basename "$1") # arch
    local BN1=$(basename $DN1) # branch/Sisyphus
    local BN2=$(basename $DN2) # p8/ALTLinux
    local BN3=$(basename $DN3) # ALTLinux/

    [ "$BN1" = "branch" ] && echo "/tmp/eepm/$BN3/$BN2/$BN1/$BN0" || echo "/tmp/eepm/$BN2/$BN1/$BN0"
}

__local_ercat()
{
   local i
   for i in "$@" ; do
       case "$i" in
           *.xz)
               a='' xzcat $i
               ;;
           *.lz4)
               a='' lz4cat $i
               ;;
           *.failed)
               # just ignore
               ;;
           *)
               cat $i
               ;;
       esac
   done
}

# something like gzip
compress_file_inplace()
{
    local OFILE="$1"
    if epm assure lz4 </dev/null ; then
        #docmd lz4 --rm "$OFILE" "$OFILE.lz4" || return
        # due old lz4
        docmd lz4 -f "$OFILE" "$OFILE.lz4" || return
        rm -fv "$OFILE"
    else
        epm assure xz </dev/null || return
        docmd xz -f "$OFILE" || return
    fi
    return 0
}

# args: url/path target_file
# result: will set FILE
download_alt_contents_index()
{
    local URL="$1"
    local TD="$2"
    local OFILE="$TD/$(basename "$URL")"

    local DONE=$(echo $OFILE*)
    # TODO: check if too old
    if [ -r "$DONE" ] ; then
        return
    fi

    mkdir -p "$TD"

    if echo "$URL" | grep -q "^file:/" ; then
        URL=$(echo "$URL" | sed -e "s|^file:||")
        [ -s "$URL" ] || { touch $OFILE.failed ; return 1; }
        ln -sf "$URL" "$OFILE" || { touch $OFILE.failed ; return 1; }
# TODO rsync
# https://bugzilla.altlinux.org/show_bug.cgi?id=14449#c22
    else
        docmd eget -O "$OFILE" "$URL" || { rm -fv $OFILE ; touch $OFILE.failed ; return 1; }
    fi

    rm -f $OFILE.failed
    compress_file_inplace "$OFILE"
}

get_local_alt_contents_index()
{
    load_helper epm-repolist

    local LOCALPATH

    epm_repolist | grep -E "rpm.*(ftp://|http://|https://|file:/)" | sed -e "s@^rpm.*\(ftp://\|http://\|https://\|file:\)@\1@g" | while read -r URL ARCH other ; do
        LOCALPATH=$(get_local_alt_mirror_path "$URL/$ARCH")
        download_alt_contents_index $URL/$ARCH/base/contents_index $LOCALPATH >&2 </dev/null || continue
        echo "$LOCALPATH/contents_index*"
    done

}

tasknumber()
{
    local num="$(echo "$1" | sed -e "s| *#*||g")"
    isnumber "$num" && echo "$*"
}

# use assure apt-repo before
get_task_packages()
{
    local tn
    for tn in $(tasknumber "$@") ; do
        docmd apt-repo list task "$tn"
    done
}