#!/bin/bash
# *******************************************************************
# **  @file eisman_localdir.sh - add packages of a local directory to db
# **
# **  Creation:     2016-08-19 dv
# **  Last update:  $Id: eisman_localdir.sh 44252 2016-12-30 23:14:30Z dv $
# **
# **  Copyright (c) 2016-2017 the eisfair team, team(at)eisfair(dot)org
# **
# **  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 2 of the License, or
# **  (at your option) any later version.
# *******************************************************************

. /var/install/include/eislib
. /var/install/include/eismanlib


# *******************************************************************
# **  show help on arguments and possible options
# **  @param  n/a
# **  @return n/a
# *******************************************************************

show_help()
{
cat <<EOF

Add packages of a local directory to packages database

usage:
    eisman localdir [OPTIONS] path_to_packages

options:
    --help      show this help screen

EOF
}


# *******************************************************************
# **  add local packages to the package database. Give them the
# **  highest priority
# **  @param  $1 local pach
# **  @param  $2 list of package info files
# **  @return n/a
# *******************************************************************

add_packages()
{
    local path="${1}"
    local packages="${2}"
    local old_ifs="${IFS}"

    # first, rename current packages db
    if ! mv "${EISMAN_PATH}/packages.db" "${EISMAN_PATH}/packages.db.org"
    then
        exit 1
    fi

    # create new empty one
    > "${EISMAN_PATH}/packages.db"

    # now add packages
    IFS="
"

    for package_info in ${packages}
    do
        local rel_path="$(echo "${package_info}" |
                          sed -e "s#${path}##g" -e 's#[.]info$##g')"
        local url_path="file://${path}${rel_path}"

        # fetch system entry
        local system=$(cat "${package_info}"       |
            sed -ne 's#[ \t]*</*system>[ \t]*##gp' |
            head -n 1)

        if [ $(expr match "${system}" "[a-zA-Z0-9\;-]*${EISMAN_SYSTEM}") -gt 0 ]
        then
            "${EISMAN_BIN_PATH}/eisman_info_to_db.sh"   \
                               --url     "${url_path}"  \
                               "${package_info}"        \
                               /tmp/tmprecfile$$

            if [ "$?" -eq "0" ]
            then
                echo "=> .${rel_path}"
                cat "/tmp/tmprecfile$$" >> "${EISMAN_PATH}/packages.db"
            fi

            rm "/tmp/tmprecfile$$"
        fi
    done

    IFS="${old_ifs}"

    # now append original package db
    cat   "${EISMAN_PATH}/packages.db.org" >> "${EISMAN_PATH}/packages.db"
    rm -f "${EISMAN_PATH}/packages.db.org"
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    local localpath=""
    local total
    local flushonly="no"

    while [ "${1:0:2}" = "--" ]
    do
        case $1 in
        --help)
            show_help
            exit 0
            ;;
        --flush-only)
            shift
            flushonly="yes"
            ;;
        *)
            echo "error: unknown option \"$1\"!" >&2
            echo ""
            show_help
            exit 1
            ;;
        esac
    done

    # read parameters
    if [ "$#" -eq 1 ]
    then
        localpath="$(realpath ${1})"
        if [ "${?}" -ne "0" ]
        then
            exit 1
        fi
    else
        echo 'error: invalid number of arguments!' >&2
        echo
        show_help
        exit 1
    fi

    # flush packages for this URL from db
    ${EISMAN_BIN_PATH}/eisman_flushurl.sh "file://${localpath}"
    if [ "${?}" -ne "0" ]
    then
        exit 1
    fi

    if [ "${flushonly}" = "no" ]
    then
        # lookup package info files, make sure they are in alphabetic order!
        # therefore we extract the basename and the full qualified name
        # then the result is sorted and afterwards the basename is stripped
        # again
        packages="$(find "${localpath}"              \
           -name "*.info"                            \
           -exec sh -c 'echo -n "$(basename {})"' \; \
           -exec echo -n "|" \;                      \
           -exec echo {} \;  |
           sort              |
           cut -d '|' -f 2- )"
        if [ "${?}" -ne "0" ]
        then
            exit 1
        fi

        # count result
        total="$(echo "${packages}" | wc -l)"

        # show what we are doing
        mecho --info "Processing ${total} package info files ..."

        # to the real job
        add_packages "${localpath}" "${packages}"

        # show that we completed the job
        mecho --info 'Done!'
    fi
}

main "${@}"
