#!/bin/bash

TMPF=/tmp/__tmpfile__
MENU=/boot/grub/menu.lst
DHCNF=/etc/dhcpd.conf
DHSNF=/etc/sysconfig/dhcpd
SUSECNF=/etc/sysconfig/suseconfig

cd /etc/pam.d
for i in *
do
    if [ "$i" != "passwd" ]; then
        x=`grep -q ^"auth    required    /lib/security/pam_hsc.so" $i`
        if [ $? -eq 1 ]; then
            echo "auth    required    /lib/security/pam_hsc.so" >> $i
        fi
    fi
done
cd /
grep -q "HMC_BASH_PROFILE_PROCESSING" /etc/profile
if [ $? -ne 0 ]; then
    if [ -f /opt/hsc/data/ssh/hmcbashprofile ]; then
        mv /etc/profile /etc/profile.tmp
        cp /opt/hsc/data/ssh/hmcbashprofile /etc/profile
        cat /etc/profile.tmp >> /etc/profile
    fi
fi

grep -q "^DisplayManager.xCrashDelta: 0" /etc/X11/xdm/xdm-config
if [ $? -ne 0 ]; then
   echo "DisplayManager.xCrashDelta: 0" >> /etc/X11/xdm/xdm-config
fi

x=`grep "^*backarrowKeyIsErase:" /usr/X11R6/lib/X11/app-defaults/XTerm 2>/dev/null`
if [ $? -eq 0 ]; then
    y=`echo $x | cut -d' ' -f2`
    if [ "$y" = "true" ]; then
        sed -e 's/^*backarrowKeyIsErase:[ \t]true/*backarrowKeyIsErase:\tfalse/g' /usr/X11R6/lib/X11/app-defaults/XTerm > /tmp/_saved_XTerm
        if [ $? -eq 0 ]; then
            mv /tmp/_saved_XTerm /usr/X11R6/lib/X11/app-defaults/XTerm
        fi
    fi
else
    echo "*backarrowKeyIsErase:\tfalse" >> /usr/X11R6/lib/X11/app-defaults/XTerm
fi
# Enable ctrl-alt-del if not already enabled
grep -q "^# ca::ctrlaltdel" /etc/inittab
if [ $? -eq 0 ]; then
    sed -e 's/^# ca::ctrlaltdel/ca::ctrlaltdel/g' /etc/inittab > /tmp/__init__
    if [ $? -eq 0 ]; then
        mv /tmp/__init__ /etc/inittab
    fi
fi
if [ -f /hmcrbin/more ]; then
    rm -f /hmcrbin/more
fi
if [ -f /usr/hmcrbin/awk ]; then
    rm -f /usr/hmcrbin/awk
fi

if [ -f /opt/hsc/data/standard_toolbar.ini ]; then
    cp -p /opt/hsc/data/standard_toolbar.ini /usr/share/opera/ini/
fi
/usr/sbin/rsct/bin/rmcctrl -q
/sbin/chkconfig -d -f hotplug   2>&1 >/dev/null
/sbin/chkconfig -d -f ipsec     2>&1 >/dev/null
/sbin/chkconfig -d -f ldap      2>&1 >/dev/null
/sbin/chkconfig -d -f named     2>&1 >/dev/null
/sbin/chkconfig -d -f nfs       2>&1 >/dev/null
/sbin/chkconfig -d -f nfsboot   2>&1 >/dev/null
/sbin/chkconfig -d -f nfslock   2>&1 >/dev/null
/sbin/chkconfig -d -f nfsserver 2>&1 >/dev/null
/sbin/chkconfig -d -f nscd      2>&1 >/dev/null
/sbin/chkconfig -d -f portmap   2>&1 >/dev/null
/sbin/chkconfig -d -f rpasswdd  2>&1 >/dev/null
/sbin/chkconfig -d -f setserial 2>&1 >/dev/null
/sbin/chkconfig -d -f slurpd    2>&1 >/dev/null
/sbin/chkconfig -d -f xfs       2>&1 >/dev/null
/sbin/chkconfig -d -f slpd       2>&1 >/dev/null

/usr/bin/chage -M 99999 root    2>&1 >/dev/null
/usr/bin/chage -M 99999 hscroot 2>&1 >/dev/null
/usr/bin/chage -M 99999 ccfw    2>&1 >/dev/null
#/usr/websm/bin/wsmserver -enable -portstart 30000 -portend 30009

if [ -f $DHCNF ]; then
    sed -e 's/^max-lease-time[ ]*7200/max-lease-time infinite/g' $DHCNF > $TMPF
    mv $TMPF $DHCNF
fi

sed -e 's/ENABLE_SUSECONFIG=yes/ENABLE_CONFIG=no/' $SUSECNF > $TMPF
cp $TMPF $SUSECNF

grep -q "drive k: file=\"\/dev\/sdb\"" /etc/mtools.conf 
if [ $? -ne 0 ] ; then
    echo "drive k: file=\"/dev/sdb\"" >> /etc/mtools.conf   
fi

grep -q "drive l: file=\"\/dev\/sda\"" /etc/mtools.conf
if [ $? -ne 0 ] ; then
    echo "drive l: file=\"/dev/sda\"" >> /etc/mtools.conf   
fi

grep -q "drive p: file=\"\/dev\/sdc\"" /etc/mtools.conf
if [ $? -ne 0 ] ; then
    echo "drive p: file=\"/dev/sdc\"" >> /etc/mtools.conf   
fi

grep -q "drive q: file=\"\/dev\/sdb1\"" /etc/mtools.conf
if [ $? -ne 0 ] ; then
    echo "drive q: file=\"/dev/sdb1\"" >> /etc/mtools.conf   
fi

grep -q "drive r: file=\"\/dev\/sdc1\"" /etc/mtools.conf
if [ $? -ne 0 ] ; then
    echo "drive r: file=\"/dev/sdc1\"" >> /etc/mtools.conf   
fi

if [ -f /opt/hsc/com/ibm/hsc/websm/launch/hscmgt/rpminst.dat ]; then
    rm -f /opt/hsc/com/ibm/hsc/websm/launch/hscmgt/rpminst.dat
fi
   
ln -sf /opt/hsc/bin/commandcontrol /opt/hsc/bin/setlparcap

# Now make sure the proper fonts is avail for BIDI support

grep -q "/usr/share/fonts/aix/aix_ar" /etc/X11/XF86Config
if [ $? -ne 0 ]; then
   >/tmp/_tmp_xf86_
   cat /etc/X11/XF86Config | awk ' BEGIN {
      while (getline $o > 0) {
        printf("%s\n",$o) >> "/tmp/_tmp_xf86_";
        if( $o ~ /\/usr\/share\/fonts\/aix\/aix_tc/ )
          printf("    FontPath   \"/usr/share/fonts/aix/aix_ar\"\n") >> "/tmp/_tmp_xf86_";
      }
   }'
   # Now copy if we think the entry is added
   grep -q "/usr/share/fonts/aix/aix_ar" /tmp/_tmp_xf86_
   if [ $? -eq 0 ]; then
      cp /tmp/_tmp_xf86_ /etc/X11/XF86Config
      rm -f /tmp/_tmp_xf86_
   fi 
fi

# Handling of UTF-8 fonts

grep -q "##HMC modified font" /etc/X11/XF86Config
if [ $? -ne 0 ]; then
   > /tmp/_tmp_xf86_
   cat /etc/X11/XF86Config | awk ' BEGIN {
      while (getline $o > 0) {
        if( $o ~ /\/usr\/X11R6\/lib\/X11\/fonts\/local\// ) {
	  printf("##HMC modified font\n") >> "/tmp/_tmp_xf86_";
          printf("    FontPath   \"/usr/share/fonts/suppl\"\n") >>"/tmp/_tmp_xf86_";
          printf("%s\n", $o) >> "/tmp/_tmp_xf86_";
        } else if ( $o ~ /\usr\/share\/fonts\/suppl/ ) {
          continue;
        } else {
          printf("%s\n", $o) >> "/tmp/_tmp_xf86_";
        }
      }
    }'          
   # Now copy if we think the entry is added
   grep -q "##HMC modified font" /tmp/_tmp_xf86_
   if [ $? -eq 0 ]; then
      cp /tmp/_tmp_xf86_ /etc/X11/XF86Config
      rm -f /tmp/_tmp_xf86_
   fi 
fi
cd /

# Handle /etc/skel directory

cd /etc/skel
skelfl=`find . -type f`
if [ "$skelfl" != "" ]; then
    cd /home/
    for i in *
    do
        cd /home/$i
        for j in $skelfl
        do
            tdf=${j#./}
            if [[ "$tdf" != ".bash_history" && "$tdf" != ".bashrc" ]]; then
                if [ -f /home/$i/$tdf ]; then
                    rm -f /home/$i/$tdf 2>/dev/null
                fi
            fi
        done
        rm -rf /home/$i/.xemacs 2>/dev/null
        rm -rf /home/$i/.Xresources 2>/dev/null
        rm -rf /home/$i/.fluxbox 2>/dev/null
        rm -rf /home/$i/Documents 2>/dev/null
        rm -rf /home/$i/public_html 2>/dev/null
    done
    rm -rf /etc/skel/*
    rm -f /etc/skel/.*
fi

grep ^DHCPD_INTERFACE= $DHSNF | cut -f2 -d '='| grep -q eth
if [ $? -ne 0 ]; then
    sed -e 's/^DHCPD_INTERFACE=""/DHCPD_INTERFACE="eth0"/' $DHSNF > $TMPF
    mv $TMPF $DHSNF
    grep ^DHCPD_INTERFACE= $DHSNF | cut -f2 -d '='| grep -q eth
    if [ $? -ne 0 ]; then
        sed -e 's/^DHCPD_INTERFACE="/DHCPD_INTERFACE="eth0 /' $DHSNF > $TMPF
        mv $TMPF $DHSNF
    fi
fi
mkdir -p /var/lib/dhcp/db

grep -q psmouse $MENU
if [ $? -ne 0 ]; then
    sed -e 's/apm=power-off/apm=power-off psmouse.proto=bare/' $MENU > $TMPF
    mv $TMPF $MENU
fi
chmod 600 $MENU

# remove dhcp-lease-time in dhclient.conf
if [ -f /etc/dhclient.conf ]; then
    /usr/bin/sed -e 's/send dhcp-lease-time 3600;//g' /etc/dhclient.conf > /tmp/newdhclient.conf
    mv /tmp/newdhclient.conf /etc/dhclient.conf
fi

# change PasswordAuthentication to yes
# change PermitRootLogin to no
SSHD_CFG="/etc/ssh/sshd_config"
if [ -f $SSHD_CFG ]; then
    /usr/bin/sed -e 's/PasswordAuthentication no/PasswordAuthentication yes/g' $SSHD_CFG > /tmp/sshd_config
    mv /tmp/sshd_config $SSHD_CFG
    grep -q "^#PermitRootLogin yes" $SSHD_CFG
    if [ $? -eq 0 ]; then
       /usr/bin/sed -e 's/^#PermitRootLogin yes/PermitRootLogin no/g' $SSHD_CFG > /tmp/sshd_config
       mv /tmp/sshd_config $SSHD_CFG
    fi
fi

grep -q "^mdns off" /etc/host.conf
if [ $? -ne 0 ]; then
   echo "mdns off" >> /etc/host.conf
fi
# link rsct jar file
ln -sf /usr/sbin/rsct/codebase/rmcapi.jar /usr/websm/codebase/pluginjars/rmcjava.jar

grep -q "^wsm:2345" /etc/inittab
if [ $? -eq 0 ]; then
    grep -v "^wsm:2345" /etc/inittab > /tmp/__wsminittab__
    if [ $? -eq 0 ]; then
        mv /tmp/__wsminittab__ /etc/inittab
    fi
fi

grep -q "^eclp" /etc/inittab
if [ $? -eq 0 ]; then
   grep -v "^eclp" /etc/inittab > /tmp/_mod_inittab_
   mv /tmp/_mod_inittab_ /etc/inittab
fi

# remove pegasus start - it will start by ccfw
if [ -f /etc/init.d/rc5.d/S16pegasus ]; then
    rm -f /etc/init.d/rc5.d/S16pegasus
fi
if [ -f /etc/init.d/rc3.d/S16pegasus ]; then
    rm -f /etc/init.d/rc3.d/S16pegasus
fi

exit 0
