#!@l_prefix@/lib/openpkg/bash
##
##  install -- OpenPKG Tool Chain "install" command (BOOSTRAP VERSION ONLY)
##  Copyright (c) 2000-2005 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2005 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  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.
##

##
##  This command emulates the "openpkg install openpkg-tools" command
##  in order to bootstrap the installation of the OpenPKG Tool Chain.
##

#   sanity check usage
if [ $# -eq 1 -a ".$1" = ".openpkg-tools" ]; then
    :
else
    echo "openpkg:ERROR: still not available functionality \"install $*\""                        1>&2
    echo "openpkg:INFO: This functionality is provided by the \"openpkg-tools\" package only!"    1>&2
    echo "openpkg:INFO: Run the command \"$OPENPKG_PREFIX/bin/openpkg install openpkg-tools\" to" 1>&2
    echo "openpkg:INFO: bootstrap the \"openpkg-tools\" package for additional functionality."    1>&2
    echo "openpkg:INFO: This bootstrap version only supports this particular install command."    1>&2
    exit 1
fi

#   determine OpenPKG release and download URLs
release=`$OPENPKG_PREFIX/bin/openpkg rpm --eval '%{l_openpkg_release -F "%s"}'`
case "$release" in
    CURRENT )
        url_src="ftp://ftp.openpkg.org/current/SRC"
        url_upd=""
        ;;
    [0-9].[0-9] )
        url_src="ftp://ftp.openpkg.org/release/$release/SRC"
        url_upd="ftp://ftp.openpkg.org/release/$release/UPD"
        ;;
    * )
        echo "openpkg:ERROR: invalid OpenPKG release \"$release\"" 1>&2
        exit 1
        ;;
esac

#   determine path to binary RPMs
rpmdir=`$OPENPKG_PREFIX/bin/openpkg rpm --eval '%{_rpmdir}'`
rpmtag=`$OPENPKG_PREFIX/bin/openpkg rpm --eval '%{l_platform -p}-%{l_tag}'`

#   iterate through package installation in dependency order
for pkg in openpkg make binutils gcc perl openpkg-tools; do
    #   determine latest package version and URL
    pkg_srpm=""
    pkg_name=""
    for url in $url_upd $url_src; do
        if [ ".$url" = . ]; then
            continue
        fi
        pkg_name=`$OPENPKG_PREFIX/lib/openpkg/curl -s -l "$url/" |\
            egrep "^${pkg}-[0-9]" |\
            sed -e "s;^;X;" \
                -e "s;^X${pkg}-\\([0-9][^-]*\\)-\\([^-][^-]*\\)\\.src\\.rpm.*;\\2:\\1;" \
                -e "s;^X${pkg}-\\([0-9][^-]*\\)-\\([^-][^-]*\\)\\.src\\.sh.*;\\2:\\1;" \
                -e "s;^X.*;;" |\
            sort -r | sed -e 'q' |\
            sed -e "s;^\\([^:]*\\):\\(.*\\);${pkg}-\\2-\\1;"`
        if [ ".$pkg_name" != . ]; then
            pkg_srpm="$url/$pkg_name.src.rpm"
            break
        fi
    done
    if [ ".$pkg_name" = . ]; then
        echo "openpkg:ERROR: package \"$pkg\" not found under URLs:" 1>&2
        if [ ".$url_upd" != . ]; then
            echo "openpkg:ERROR: $url_upd" 1>&2
        fi
        echo "openpkg:ERROR: $url_src" 1>&2
        exit 1
    fi

    #   check whether package is already installed
    if [ ".`$OPENPKG_PREFIX/bin/openpkg rpm -q $pkg_name | grep -v 'is not installed'`" != . ]; then
        continue
    fi

    #   build binary package from source package
    if [ ! -f "$rpmdir/$pkg_name.$rpmtag.rpm" ]; then
        echo "++ fetching and building source package \"$pkg_name.src.rpm\""
        $OPENPKG_PREFIX/bin/openpkg rpm --rebuild $pkg_srpm
        if [ $? -ne 0 ]; then
            echo "openpkg:ERROR: failed to build package \"$pkg_name\" (RPM returned with $?)" 1>&2
            exit 1
        fi
        if [ ! -f "$rpmdir/$pkg_name.$rpmtag.rpm" ]; then
            echo "openpkg:ERROR: failed to build package \"$pkg_name\" (RPM returned successfully but there is no $rpmdir/$pkg_name.$rpmtag.rpm)" 1>&2
            exit 1
        fi
    fi

    #   install binary package
    echo "++ installing binary package \"$pkg_name.$rpmtag.rpm\""
    $OPENPKG_PREFIX/bin/openpkg rpm -Uvh "$rpmdir/$pkg_name.$rpmtag.rpm"
    if [ $? -ne 0 ]; then
        echo "openpkg:ERROR: failed to install package \"$pkg_name\" (RPM returned with $?)" 1>&2
        exit 1
    fi
done

#   display final result message
if [ -d "$OPENPKG_PREFIX/lib/openpkg-tools" ]; then
    ( echo "Congratulations, you have bootstrapped the OpenPKG Tool Chain."
      echo "You now have additional management commands available. Run"
      echo "    \$ $OPENPKG_PREFIX/bin/openpkg --help"
      echo "for more details on the actual list of available commands."
    ) | $OPENPKG_PREFIX/lib/openpkg/rpmtool msg -b -t notice
else
    echo "openpkg:ERROR: failed to install package \"openpkg-tools\"" 1>&2
    exit 1
fi

