#!/bin/sh

F=/usr/lib/X11/xinit/xinitrc
OF=/usr/lib/X11/xinit/Xclients

# check if the command is already present:
if grep coolicon $OF >/dev/null 2>&1 | grep coolicon $F >/dev/null 2>&1 ; then
    exit 0
fi

TF=/tmp/cooledit-install

cat $F | sed -e '/^if.*Xclients/,$D' > $TF

# estimate the speed of this machine:
BOGOMIPS=`cat /proc/cpuinfo | grep bogomips | head -1 | cut -f2 -d: | cut -f1 -d.`
if test -z "$BOGOMIPS" ; then
    BOGOMIPS=50
fi
if test "$BOGOMIPS" -gt "500" ; then
    BOGOMIPS=500
fi
# add use of shape extension if this is a fast machine:
if test "$BOGOMIPS" -gt "80" ; then
    OPTIONS="-s -X $BOGOMIPS"
else
    OPTIONS="-X $BOGOMIPS"
fi

cat >> $TF <<EOF
# start coolicon
if coolicon --version >/dev/null 2>&1 ; then
    if test -f \$MAIL ; then
	MFILE=\$MAIL
    elif test -f /var/spool/mail/\$LOGNAME ; then
	MFILE=/var/spool/mail/\$LOGNAME
    elif test -f /var/mail/\$LOGNAME ; then
	MFILE=/var/mail/\$LOGNAME
    else
	MFILE=""
    fi
    if test -f \$MFILE ; then
	coolicon $OPTIONS -M \$MFILE 2>&1 | coolmessage &
    else
	coolicon $OPTIONS 2>&1 | coolmessage &
    fi
fi

EOF

sed -e '/^if.*Xclients/,$!D' $F >> $TF

cp $F $F.pre-coolicon
cat $TF > $F
rm $TF

exit 0

