#! /bin/sh

# $Progeny$

# Copyright 2002 Hewlett-Packard Company
# Copyright 2004 Progeny Linux Systems, Inc.
# Copyright 2006 Petter Reinholdtsen
#
# Based on discover-modprobe, modified to install debian packages instead
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

. /usr/share/debconf/confmodule

set -e

# Too bad we don't have something like sysexits.h for POSIX sh...

EX_USAGE=64

# These defaults are only used if discover-config can not be found.
# This is the case if /usr isn't mounted yet.

sysconfdir=/etc
localstatedir=/var
discover=discover
types="all"

if [ -x /usr/bin/discover-config ]; then
    sysconfdir="$(discover-config --sysconfdir)"
    localstatedir="$(discover-config --localstatedir)"
fi

if [ -x /sbin/discover ]; then
    discover=/sbin/discover
elif [ -x /usr/bin/discover ]; then
    discover=/usr/bin/discover
elif [ -x /bin/discover-static ]; then
    discover=/bin/discover-static
fi

conf="${sysconfdir}/discover-aptinstall.conf"

[ -e ${conf} ] && . "${conf}"

usage ()
{
    cat <<EOF
usage: $0 [-lnv]
EOF
}

###############################################################################

nop=
verbose=false
listonly=false
while getopts lnv ch; do
    case $ch in
    l)
        listonly=true
        ;;
    n)
        nop=echo
        ;;
    v)
        verbose=true
        ;;
    ?)
        usage
        exit ${EX_USAGE}
    esac
done
shift $((${OPTIND} - 1))

###############################################################################

${verbose} && printf "Discovering hardware: "

if [ -e /etc/debian_version ] ; then
    version="$(cat /etc/debian_version)"
    if [ testing/unstable != "$version" ] ; then 
        dataversion="-data-version=$(cat /etc/debian_version)"
    fi
fi

packages=$(${discover} --data-path=package/debian/name \
	${dataversion} ${types} | grep -E -v '^ *$' | sort -u)

${verbose} && echo ${packages}

if [ "$packages" ] ; then
    if [ true = "$listonly" ] ; then
        echo $packages
    else
        pkglist="`echo $packages | sed 's/ /,/'`"
        db_subst discover/install_hw_packages PACKAGES "$pkglist"
        db_set discover/install_hw_packages "$pkglist"
        db_fset discover/install_hw_packages seen false
        db_input high discover/install_hw_packages || [ $? -eq 30 ]
        db_go
        db_get discover/install_hw_packages
        packages="`echo $RET | sed 's/,//'`"
        db_stop
        if [ "$packages" ] ; then
            $nop aptitude install -y $packages
        fi
    fi
else
    ${verbose} && echo "No hardware specific packages found for this machine"
fi

exit 0
