#!@l_prefix@/lib/openpkg/bash
##
##  rpm -- OpenPKG Registry "rpm" overload command
##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
##
##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
##  All rights reserved. Licenses which grant limited permission to use,
##  copy, modify and distribute this software are available from the
##  OpenPKG GmbH.
##
##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   determine path to OpenPKG instance
PREFIX="@l_prefix@"
if [ ".${OPENPKG_PREFIX}" != . ]; then
    PREFIX="${OPENPKG_PREFIX}"
fi

#   determine rpm
rpm="$PREFIX/libexec/openpkg/rpm"
[ -x "$PREFIX/lib/openpkg/rpm" ] && rpm="$PREFIX/lib/openpkg/rpm"

#   determine whether URL rewriting is active
rewrite=0
#$PREFIX/bin/openpkg register --printstatus >/dev/null 2>&1 && rewrite=1

#   main
if [ $rewrite = 1 ]; then
    #   URL rewriting
    declare -a a
    declare -i i=0
    while [ ".$1" != . ]; do
        arg="$1"
        printf '%s\n' "$arg" | grep :// >/dev/null && arg=`$PREFIX/bin/openpkg register --rewriteurls "$arg"`
        a[$i]="$arg"
        i=i+1
        shift
    done
    set -- "${a[@]}"
    unset arg i a
else
    #   stealth mode
    :
fi

#   execute underlying application
exec $rpm "$@"

