#!/bin/sh
##
##  aux.prereq.sh -- Platform Pre-Requisite Checks
##  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.
##
##  Usage: aux.prereq.sh source|binary
##

mode="$1"

#   determine platform ids
platform_prod=`sh ./shtool platform -n -L -S "" -C "+" -F "%<ap>-%<sp>"`
platform_tech=`sh ./shtool platform -n -L -S "" -C "+" -F "%<at>-%<st>"`

#
#   CHECK: amount of platform support
#
case "$platform_prod:$product_tech" in
    #   official "forecasted" platforms
    *-aix*:*              |\
    *-irix*:*             )
        support=forecasted
        ;;
    #   official "tentative" platforms
    ia64-freebsd5.4:*     |\
    ix86-gentoo1.12.0:*   |\
    ix86-mandriva10.2:*   |\
    ix86-freebsd7.0:*     )
        support=tentative
        ;;
    #   official "supported" platforms
    ix86-freebsd5.4:*     |\
    ix86-debian3.1:*      |\
    ix86-solaris9:*       |\
    amd64-freebsd5.4:*    |\
    sparc64-solaris9:*    |\
    ix86-netbsd2.0.2:*    |\
    ix86-fedora4:*        |\
    ix86-suse10.0:*       |\
    sparc64-solaris10:*   |\
    ix86-solaris10:*      |\
    ix86-freebsd6.0:*     |\
    ix86-rhel4:*          )
        support=supported
        ;;
    #   official "obsolete" platforms
    sparc64-solaris8:*    |\
    ix86-suse9.3:*        |\
    ix86-freebsd4.11:*    )
        support=obsolete
        ;;
    #   official "deprecated" platforms
    *-tru64*:*            |\
    *-unixware*:*         )
        support=deprecated
        ;;
    #   probable platforms
    hppa-hpux11.11:*      |\
    ppc-darwin7.**:*      |\
    *-debian[23].*:*      |\
    *-fedora*:*           |\
    *-freebsd[456].*:*    |\
    *-gentoo1.*:*         |\
    *-hpux11.*:*          |\
    *-mandrake*:*         |\
    *-netbsd*:*           |\
    *-rhel3:*             |\
    *-rhl9:*              |\
    *-solaris*:*          |\
    *-suse[789].*:*       )
        support=probable
        ;;
    #   unknown platforms
    *:* )
        support=unknown
        ;;
esac
case "$support" in
    forecasted ) support="forecasted (still not known to work)"     ;;
    tentative  ) support="tentative (should already work)"          ;;
    supported  ) support="supported"                                ;;
    obsolete   ) support="obsolete (should still work)"             ;;
    deprecated ) support="deprecated (no longer known to work)"     ;;
    probable   ) support="probable (untested but expected to work)" ;;
    unknown    ) support="unknown (expect not to work)"             ;;
esac
echo "++ platform product:    $platform_prod"
echo "++ platform technology: $platform_tech"
echo "++ platform support:    $support"

#
#   CHECK: diskspace requirement
#
if [ ".$mode" = .source ]; then
    fs_path="${TMPDIR-/tmp}"
    fs_need=150000
else
    fs_path="" # FIXME
    fs_need=0  # FIXME
fi
fs_free=""
case "$platform_tech" in
    *-freebsd* | *-linux* | *-sunos* )
       fs_free=`cd $fs_path && /bin/df -k . | sed -n -e '$p' | sed -e 's;^[^ ]*;;' -e 's; *;;' | awk '{ print $3; }'`
       ;;
esac
if [ ".$fs_free" != . ]; then
    if [ $fs_free -lt $fs_need ]; then
        if [ ".$mode" = .source ]; then
            echo "ERROR: temporary directory \"$fs_path\" has to reside on a partition" 1>&2
            echo "       with at least $fs_need KB of free disk space. Set \$TMPDIR to" 1>&2
            echo "       a directory on a partition with enough free disk space, please." 1>&2
        else
            echo "ERROR: installation directory \"$fs_path\" has to reside on a partition" 1>&2
            echo "       with at least $fs_need KB of free disk space. Make \"$fs_path\" a" 1>&2
            echo "       symbolic link to a directory on a partition with enough free" 1>&2
            echo "       disk space, please." 1>&2
        fi
        exit 1
    fi
fi

#
#   CHECK: available vendor packages
#
#   ...FIXME...

#
#   CHECK: available tools in $PATH
#
#   ...FIXME...

#
#   CHECK: available devices /dev/random, etc.
#
#   ...FIXME...

#
#   CHECK: consistency check for /prefix (symlink!)
#
#   ...FIXME...

