#!/bin/sh

# default values

prefix=/usr/local
kernel_dir=/usr/src/linux
linpsm_module=linpsm
linpsm_file=/dev/linpsm
lpt_port=0x378
linpsm_perm=666
linpsm_owner=root
linpsm_group=root

#----------------------------------------------------------------------
# subroutines

xuname_linux() {
    if [ -f /etc/debian_version ]; then
	uname_dist=Debian
    elif [ -f /etc/redhat-release ]; then
	uname_dist=RedHat
    elif [ -f /etc/turbolinux-release ]; then
	uname_dist=turboLinux
    elif [ -f /etc/SuSE-release ]; then
	uname_dist=SuSE
    fi

    # GNU uname
    uname_cpu=`uname -m` # maybe inaccurate
    uname_plathome=`uname -m`
}

#----------------------------------------------------------------------
xuname_sunos() {
    case $uname_rel in
    4*)
	uname_dist=SunOS
	uname_cpu=`uname -m` # maybe inaccurate
	uname_plathome=`uname -m`
	;;
    5*)
	uname_dist=Solaris
	uname_cpu=`uname -p`
	uname_plathome=`uname -i`
	;;
    esac
}

#----------------------------------------------------------------------
xuname_cygwin() {
    uname_dist=Cygwin
    uname_cpu=`uname -m`
    uname_plathome=Cygwin
}


#----------------------------------------------------------------------
# Name
#	xuname - set OS-dependent shell variables
#
# Variables
#	uname_os: SunOS, Linux,...
#	uname_rel: 5.5.1, 2.2.14, ... (*1)
#	uname_cpu: i[3456]86, sparc, alpha, ...
#	uname_plathome: x86pc, sun4m, axp, ...
#	uname_dist: Debian, RedHat, SuSE, Solaris, ...
#
# (*1) `rel' refers kernel release, not distribution.
# (*2) xuname() may return 'unknown'
#
xuname() {
    uname_os=`uname -s`
    uname_rel=`uname -r`
    uname_cpu=unknown
    uname_plathome=unknown
    uname_dist=unknown

    case `uname -s` in
    SunOS)
	xuname_sunos ;;
    Linux)
	xuname_linux ;;
    CYGWIN*)
	xuname_cygwin ;;
    esac
}

#----------------------------------------------------------------------
# Name
#	find_init_dir -- find base directory for sysv init scripts
#	and set shell variable $init_dir.
#
# Description
#	This routine set shell variable "init_dir" to directory
#	where sysv init scripts live in.
#
#	sysv init scripts live in "$init_dir/init.d/"
#	start-up and kill file (S[0-9][0-9]* and K[0-9][0-9]*) live in
#	"$init/rcN.d".
#	In most system, start-up and kill file is symbolic link.
#
#	This routine must be called after xuname().
#
find_init_dir() {
    case "$uname_os,$uname_dist" in
    SunOS,Solaris)
	init_dir=/etc
	;;
    Linux,Debian)
	init_dir=/etc
	;;
    Linux,RedHat|Linux,turboLinux|Linux,SuSE)
	init_dir=/etc/rc.d
	;;
    esac
}

#----------------------------------------------------------------------
# analyze command options

for opt
do
    case $opt in
    --prefix=*)
	prefix=`echo "$opt" | sed s/^--prefix=//` ;;
    --kernel-dir=*)
	kernel_dir=`echo "$opt" | sed s/^--kernel-dir=//` ;;
    --linpsm-module=*)
	linpsm_module=`echo "$opt" | sed s/^--linpsm_module=//` ;;
    --linpsm-file=*)
	linpsm_file=`echo "$opt" | sed s/^--linpsm-file=//` ;;
    --lpt-port=*)
	lpt_port_=`echo "$opt" | sed s/^--lpt-port=//` ;;
    --linpsm_perm=*)
	linpsm_perm=`echo "$opt" | sed s/^--linpsm-perm=//` ;;
    --linpsm_owner=*)
	linpsm_owner=`echo "$opt" | sed s/^--linpsm-owner=//` ;;
    --linpsm-group=*)
	linpsm_group=`echo "$opt" | sed s/^--linpsm-group=//` ;;
    esac
done


#----------------------------------------------------------------------
if [ ! -d "$kernel_dir" -o ! -d "$kernel_dir/include/linux" ]; then
    echo "$0: kernel directory $kernel seems inapropriate"
    exit 1
fi

#----------------------------------------------------------------------
xuname
find_init_dir

echo "prefix... $prefix"
echo "kernel source... $kernel_dir"
echo "linpsm module name... $linpsm_module"
echo "linpsm special file... $linpsm_file"
echo "linpsm port... $lpt_port"
echo "mode of $linpsm_file... $linpsm_perm"
echo "owner of $linpsm_file... $linpsm_owner"
echo "group of $linpsm_file... $linpsm_group"
echo "Linux distribution... $uname_dist"
echo "SystemV-style init dir... $init_dir"

#----------------------------------------------------------------------
for i in *.in
do
    #dest=`echo $i | sed s/\.in$//`
    dest=`basename "$i" .in`
    echo "creating $dest..."
    sed -e s,@prefix@,$prefix,g \
	-e s,@kernel_dir@,$kernel_dir,g \
	-e s,@linpsm_module@,$linpsm_module,g \
	-e s,@linpsm_file@,$linpsm_file,g \
	-e s,@lpt_port@,$lpt_port,g \
	-e s,@linpsm_perm@,$linpsm_perm,g \
	-e s,@linpsm_owner@,$linpsm_owner,g \
	-e s,@linpsm_group@,$linpsm_group,g \
	-e s,@init_dir@,$init_dir,g \
	$i > $dest
done

echo "configure finished"
