#!/bin/sh
##
##  etc.prereq.sh -- Platform Prerequisite Checks
##  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.
##
##  Usage: etc.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* | *-netbsd* | *-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...

