#!/bin/bash
# Post-installation script for irda-utils
# Copyright 2003,2004 Sebastian Henschel <shensche@debian.org>
# This file is hereby placed into the public domain.

PACKAGE="irda-utils"
CONFIG="/etc/default/$PACKAGE"
MODFILE_24="/etc/modutils/$PACKAGE"
MODFILE_26="/etc/modprobe.d/$PACKAGE"

set -e
. /usr/share/debconf/confmodule


write_config_modules () {
    if [ ! -e $MODFILE_24 ]; then
        cat <<EOF > $MODFILE_24
alias tty-ldisc-11 irtty
alias char-major-161 ircomm-tty
alias char-major-60 ircomm_tty


# For dongles
alias irda-dongle-0 tekram
alias irda-dongle-1 esi
alias irda-dongle-2 actisys
alias irda-dongle-3 actisys
alias irda-dongle-4 girbil
alias irda-dongle-5 litelink
alias irda-dongle-6 airport
alias irda-dongle-7 old_belkin
alias irda-dongle-11 ma600


# For FIR device
EOF
    fi

    if [ ! -e $MODFILE_26 ]; then
        cat <<EOF > $MODFILE_26
# Other aliases are defined in the modules themselves
alias char-major-10-187 irnet


# For FIR device

EOF
    fi

    # get configuration data
    db_get $PACKAGE/firdev
    firdev="$RET"
    firdev26="$firdev"
    [ "$firdev" = "smc-ircc" ] && firdev26="smsc-ircc2"
    [ "$firdev" = "toshoboe" ] && firdev26="donauboe"
    db_get $PACKAGE/firopt
    firopt="$RET"


    # remove old options/aliases
    cp -a -f $MODFILE_24 $MODFILE_24.tmp
    cp -a -f $MODFILE_26 $MODFILE_26.tmp

    sed -e "
        s/.*options.*//g
        s/.*alias irda0.*//g
    " < $MODFILE_24 > $MODFILE_24.tmp
    cat -s $MODFILE_24.tmp > $MODFILE_24
    rm $MODFILE_24.tmp

    sed -e "
        s/.*options.*//g
        s/.*alias irda0.*//g
    " < $MODFILE_26 > $MODFILE_26.tmp
    cat -s $MODFILE_26.tmp > $MODFILE_26
    rm $MODFILE_26.tmp


    # insert new options/aliases
    if [ "$DEVICE" = "serial" ]; then
        echo "#options $firdev $firopt" >> $MODFILE_24
        echo "#alias irda0 $firdev" >> $MODFILE_24
        echo "#options $firdev26 $firopt" >> $MODFILE_26
        echo "#alias irda0 $firdev26" >> $MODFILE_26
    else
        echo "options $firdev $firopt" >> $MODFILE_24
        echo "alias irda0 $firdev" >> $MODFILE_24
        echo "options $firdev26 $firopt" >> $MODFILE_26
        echo "alias irda0 $firdev26" >> $MODFILE_26
    fi

    update-modules # will be removed after modutils got removed.
}



write_config_default () {
    if [ ! -e $CONFIG ]; then # create new configuration file
        cat <<EOF > $CONFIG
# Set your startup settings for irattach, the IrDA-daemon, here.

# Set this to 'false' if you do not need to start irattach. Otherwise set it
# to 'true'.
ENABLE=

# Set discovery mode which usually is a good idea for finding other devices.
# If set 'true' or 'false' irattach and sysctl are used to enable and disable
# discovery mode. By default discover mode is disabled.
DISCOVERY=

# Set IRDA device to access (e.g. /dev/ttyS1 or irda0).
# In case of irda0, the proper module for FIR-mode has to be set in
# $MODFILE_24 (2.4) or $MODFILE_26 (2.6)
DEVICE=

# Set dongle type, e.g. none, tekram, esi, actisys, actisys+, ep7211, girbil,
# litelink, airport, old_belkin, mcp2120, act200l, ma600). You do not need
# a dongle for FIR mode.
DONGLE=

# Set the serial device to quiet with setserial. This is only useful on some
# machines in FIR-mode, so most people should leave it blank. See 
# README.Debian for more information.
SETSERIAL=

# Some laptops (Toshiba Satellites and others with SMCS LPC47N227) require
# running smcinit to initialize the irda device prior to use.
# If you device is one of them, set this option to "yes"
USE_SMCINIT="no"
EOF
    fi

    # read config
    db_get $PACKAGE/enable
    ENABLE="$RET"
    db_get $PACKAGE/discovery
    DISCOVERY="$RET"
    if [ "$DEVICE" = "serial" ]; then
        db_get $PACKAGE/ttydev
        DEVICE="$RET"
    else
        DEVICE="irda0"
    fi
    db_get $PACKAGE/dongle
    DONGLE="$RET"
    db_get $PACKAGE/setserial
    SETSERIAL="$RET"

    # backup for $CONFIG with preserved ownership and permissions
    cp -a -f $CONFIG $CONFIG.tmp

    # re-insert values deleted in $CONFIG but existant in debconf
    test -z "$ENABLE" || grep -Eq '^ *ENABLE=' $CONFIG || echo "ENABLE=" >> $CONFIG
    test -z "$DISCOVERY" || grep -Eq '^ *DISCOVERY=' $CONFIG || echo "DISCOVERY=" >> $CONFIG
    test -z "$DEVICE" || grep -Eq '^ *DEVICE=' $CONFIG || echo "DEVICE=" >> $CONFIG
    test -z "$DONGLE" || grep -Eq '^ *DONGLE=' $CONFIG || echo "DONGLE=" >> $CONFIG
    test -z "$SETSERIAL" || grep -Eq '^ *SETSERIAL=' $CONFIG || echo "SETSERIAL=" >> $CONFIG

    # replace values of configuration variables in config file, preserving
    # all comments and other variables defined by the admin
    sed -e "
        s#^ *ENABLE=.*#ENABLE=\"$ENABLE\"#
        s#^ *DISCOVERY=.*#DISCOVERY=\"$DISCOVERY\"#
        s#^ *DEVICE=.*#DEVICE=\"$DEVICE\"#
        s#^ *DONGLE=.*#DONGLE=\"$DONGLE\"#
        s#^ *SETSERIAL=.*#SETSERIAL=\"$SETSERIAL\"#
    " < $CONFIG > $CONFIG.tmp
    mv -f $CONFIG.tmp $CONFIG
}




if [ "$1" = "configure" ]; then

    cd /dev && ./MAKEDEV irda && ./MAKEDEV irnet

    db_get $PACKAGE/selectdevice
    DEVICE=$RET

    write_config_modules
    write_config_default
fi

#DEBHELPER#

exit 0
