#!/bin/sh
##
##  aux.prereq.sh -- Platform Pre-Requisite Checks
##  Copyright (c) 2000-2006 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2006 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"

#   CHECK: 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>"`
echo "++ platform product:    $platform_prod"
echo "++ platform technology: $platform_tech"

#
#   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...

