#!/bin/bash
# *******************************************************************
# **  @file eisman_flushurl.sh - flush an url from the packages database
# **
# **  Creation:     2016-08-17 dv
# **  Last update:  $Id: eisman_flushurl.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

Flush an url from the packages database

usage:
    eisman flushurl [OPTIONS] url

options:
    --help      show this help screen

EOF
}


# *******************************************************************
# **  remove package from database of installed files
# **  @param  $1  package name
# **  @return n/a
# *******************************************************************

remove_url_from_database()
{
    local url="${1}"

    # grep all packages other then "$package" into temporary file
    grep -v -e "^\[.*\]\[.*\]\[.*\]\[.*\]\[${url}.*\]" \
       "${EISMAN_PATH}/packages.db" > "${EISMAN_PATH}/packages.db.tmp"

    if [ "$?" -eq 0 ]
    then
        # remove database file
        rm -f "${EISMAN_PATH}/packages.db"

        # move temporary file to new database
        mv "${EISMAN_PATH}/packages.db.tmp" "${EISMAN_PATH}/packages.db"
    else
        echo "error: unable to delete URL from database!" >&2
        exit 1
    fi
}


# *******************************************************************
# **  main program
# *******************************************************************
main()
{
    while [ "${1:0:2}" = "--" ]
    do
        case $1 in
        --help)
            show_help
            exit 0
            ;;
        *)
            echo "error: unknown option \"$1\"!" >&2
            echo ""
            show_help
            exit 1
            ;;
        esac
    done

    # read parameter
    url="$1"
    if [ -z "${url}" ]
    then
        echo "error: missing URL parameter!" >&2
        echo
        show_help
        exit 1
    fi

    remove_url_from_database "${url}"
    exit 0
}

main "${@}"
