#! /bin/sh

CLIENTS="(solaris aix hpux linux freebsd drsnx)" # supported clients

LOGFILES="/var/adm/messages /var/adm/sulog /var/log/secure /var/log/messages /var/adm/syslog/syslog.log"

CUSTOM="agents bcnunet bcnud_server bcnulocal hostinfo logpatterns services"

USAGE="client_install -h bcnu_home_dir -c client_type -s startup_at_boot -l Logfiles -d delete_other_platforms"

while getopts h:c:s:l:d: x
do
        case $x in
        h)      BCNUHOME=$OPTARG;;
        c)      client=$OPTARG;;
        s)      startup=$OPTARG;;
        l)      logfiles=$OPTARG;;
        d)      delete=$OPTARG;;
        \?)     echo "client_install : syntax error "
                printf $USAGE
                exit 2;;
        esac
done

if [ -z "$BCNUHOME" ]
then
	if [ -d /usr/local/bcnu ]
	then
	        BCNUHOME="/usr/local/bcnu"
	else
	        printf "Enter directory name of bcnu home:[/usr/local/bcnu] "
	        read home
	        if [ -z "$home" ]
	                then
	                      BCNUHOME="/usr/local/bcnu"
	                else
	  	              BCNUHOME=$home
	        fi
	fi
fi

cd $BCNUHOME

. $BCNUHOME/etc/bcnuenv.distrib

if [ -z "$client" ]
then
	printf "Enter client type $CLIENTS :[solaris] "
	read client
fi
if [ -z "$client" ]
	then
		BCNUSERVER="solaris"
	else
		BCNUSERVER=$client
fi

. $BCNUHOME/etc/bcnuenv.$BCNUSERVER

if [ -f $BCNUHOME/etc/bcnulocal ]
	then
		. $BCNUHOME/etc/bcnulocal 	# to allow for localizations
fi

sed "s/BCNUSERVER=solaris/BCNUSERVER=$BCNUSERVER/g" $BCNUHOME/etc/bcnuenv.distrib >$BCNUHOME/etc/bcnuenv
mv $BCNUHOME/etc/bcnud_server $BCNUHOME/etc/bcnud_server.old 2>/dev/null

for custom in $CUSTOM
do

if [ -f $BCNUHOME/etc/$custom ]
	then
		echo "$BCNUHOME/etc/$custom: already exists - unchanged"
	else
		sed "s+/usr/local/bcnu+$BCNUHOME+g" $BCNUHOME/etc/$custom.distrib  >$BCNUHOME/etc/$custom
		echo "$BCNUHOME/etc/$custom: copied"
		chmod 700 $BCNUHOME/etc/$custom
#		echo "Please edit before starting bcnu server"
fi
done

if [ -f $BCNUHOME/etc/bcnud.pid ]
	then
		echo "Stopping bcnud"
		$BCNUHOME/etc/bcnud_server stop
fi
cd $BCNUHOME/bin
rm bcnu* 2>/dev/null
cp ../platform/bcnu.$BCNUSERVER bcnu.$BCNUSERVER
cp ../platform/bcnud.$BCNUSERVER bcnud.$BCNUSERVER
cp ../platform/bcnumsg.$BCNUSERVER bcnumsg.$BCNUSERVER
ln -s bcnu.$BCNUSERVER bcnu
ln -s bcnud.$BCNUSERVER bcnud
ln -s bcnumsg.$BCNUSERVER bcnumsg
chmod 700 bcnu*

if [ -z "$startup" ]
then
	printf "Do you want to start bcnu automatically at system startup? y/n "
	read yorn
else
	yorn="$startup"
fi
if [ "y" = "$yorn" ]
	then

	case "$BCNUSERVER" in
	solaris|drsnx) 	rm /etc/rc0.d/K*bcnu /etc/rc1.d/K*bcnu /etc/rc2.d/K*bcnu /etc/rc2.d/S*bcnu 2>/dev/null
			ln -s $BCNUHOME/etc/bcnud_server /etc/rc2.d/S99bcnu
			ln -s $BCNUHOME/etc/bcnud_server /etc/rc0.d/K10bcnu
			ln -s $BCNUHOME/etc/bcnud_server /etc/rc1.d/K10bcnu
			ln -s $BCNUHOME/etc/bcnud_server /etc/rc2.d/K10bcnu
			;;
	linux) 		DISTRIB=any # default setting
			# this distribution detection
			if [ -f /etc/rc.config ]
			   then
				if grep "ENABLE_SUSECONFIG=" /etc/rc.config >/dev/null
				   then
					# SuSE Distribution detected
					DISTRIB=suse
				fi
			fi
			case $DISTRIB in
			any)
			  rm /etc/rc.d/rc*.d/*bcnu 2>/dev/null
			  ln -s $BCNUHOME/etc/bcnud_server /etc/rc.d/rc3.d/S99bcnu
			  ln -s $BCNUHOME/etc/bcnud_server /etc/rc.d/rc3.d/K99bcnu
			  ln -s $BCNUHOME/etc/bcnud_server /etc/rc.d/rc5.d/S99bcnu
			  ln -s $BCNUHOME/etc/bcnud_server /etc/rc.d/rc5.d/K99bcnu
			  ln -s $BCNUHOME/etc/bcnud_server /etc/rc.d/rc0.d/K99bcnu
			  ;;
			suse)
			  rm -f /sbin/init.d/rc*.d/*bcnu
			  rm -f /sbin/init.d/bcnu

			  # we create a SuSE conform startup script
			  cat >/sbin/init.d/bcnu <<EOF
#!/bin/bash

. /etc/rc.config

# set startup if not set in rc.config 
START_BCNU=\${START_BCNU:=yes}

# Determine the base and follow a runlevel link name.
base=\${0##*/}
link=\${base#*[SK][0-9][0-9]}

# Force execution if not called by a runlevel directory.
test \$link = \$base && START_BCNU="yes"
test "\$START_BCNU" = yes || exit 0

# The echo return value for success (defined in /etc/rc.config).
return=\$rc_done


case "\$1" in
    start)
 echo -n "Starting service BCNU"
 $BCNUHOME/etc/bcnud_server start || return=\$rc_failed
 echo -e "\$return"

    ;;
    stop)
 echo -n "Shutting down service BCNU"
 $BCNUHOME/etc/bcnud_server stop || return=\$rc_failed
 echo -e "\$return"
    ;;
    restart)
 echo -n "Shutting down service BCNU"
 $BCNUHOME/etc/bcnud_server restart  ||  return=\$rc_failed
    ;;
    status)
        echo -n "Checking for service BCNU: "
 $BCNUHOME/bin/bcnu -a show localhost || return=\$rc_failed
 echo -e "\$return"
    ;;
    *)
    echo "Usage: \$0 {start|stop|restart|status}"
    exit 1
esac


# Inform the caller not only verbosely and set an exit status.
test "\$return" = "\$rc_done" || exit 1
exit 0
EOF
			  chmod u+x /sbin/init.d/bcnu
			  for RUNLEVEL in 2 3
			  do
			    cd /sbin/init.d/rc${RUNLEVEL}.d
			    # start at last
			    ln -s ../bcnu S99bcnu
			    # kill at first
			    ln -s ../bcnu K10bcnu
			  done
			  ;;
			esac
			;;
	aix)		if grep "^$BCNUHOME/etc/bcnud_server" /etc/rc.tcpip
			then 
				cp /etc/rc.tcpip /etc/rc.tcpip.bcnu
				sed "s/.*bcnud_server.*/#&/g" /etc/rc.tcpip.bcnu >/etc/rc.tcpip
			fi
			if ! grep "$BCNUHOME/etc/bcnud_server" /etc/inittab >/dev/null
			then 
				cp /etc/inittab /etc/inittab.bcnu
				mkitab "bcnu:2:once:$BCNUHOME/etc/bcnud_server start >/dev/console 2>&1 # BCNU startup"
			fi
			if [ -s /etc/rc.shutdown ]
			   then
				if ! grep "$BCNUHOME/etc/bcnud_server" /etc/rc.shutdown >/dev/null
				   then 
					cp /etc/rc.shutdown /etc/rc.shutdown.bcnu
					# insert BCNU shutdown
					awk -v BCNUHOME=$BCNUHOME '/^[\/.a-zA-Z]/ {
  if ( DONE != 1 ) {
    printf "\n# BCNU shutdown\n%s/etc/bcnud_server stop\n\n%s\n", BCNUHOME, $0;
    DONE=1;
    next;
  }
}
{
  printf "%s\n", $0;
}
END {
  if ( DONE != 1 ) {
    printf "\n# BCNU shutdown\n%s/etc/bcnud_server stop\n", BCNUHOME;
    DONE=1;
  }
  
}' /etc/rc.shutdown.bcnu >/etc/rc.shutdown
				fi
			else
					printf "#!/bin/sh\n\n# BCNU shutdown\n$BCNUHOME/etc/bcnud_server stop\n" >/etc/rc.shutdown
					chmod u+x /etc/rc.shutdown
			fi
			;;
	freebsd)	echo "#! /bin/sh" >/usr/local/etc/rc.d/bcnu.sh
			echo "$BCNUHOME/etc/bcnud_server start" >>/usr/local/etc/rc.d/bcnu.sh
			chmod 755 /usr/local/etc/rc.d/bcnu.sh
			if [ -s /etc/rc.shutdown ]
			   then
				if ! grep "$BCNUHOME/etc/bcnud_server" /etc/rc.shutdown >/dev/null
				   then 
					cp /etc/rc.shutdown /etc/rc.shutdown.bcnu
					# insert BCNU shutdown
					awk -v BCNUHOME=$BCNUHOME '/^[\/.a-zA-Z]/ {
  if ( DONE != 1 ) {
    printf "\n# BCNU shutdown\n%s/etc/bcnud_server stop\n\n%s\n", BCNUHOME, $0;
    DONE=1;
    next;
  }
}
{
  printf "%s\n", $0;
}
END {
  if ( DONE != 1 ) {
    printf "\n# BCNU shutdown\n%s/etc/bcnud_server stop\n", BCNUHOME;
    DONE=1;
  }
  
}' /etc/rc.shutdown.bcnu >/etc/rc.shutdown
				fi
			else
					printf "#!/bin/sh\n\n# BCNU shutdown\n$BCNUHOME/etc/bcnud_server stop\n" >/etc/rc.shutdown
					chmod u+x /etc/rc.shutdown
			fi
			;;
	hpux)		if [ -f /etc/rc ]
			then # hpux9
			if ! grep "$BCNUHOME/etc/bcnud_server" /etc/rc
				then 
					cp /etc/rc /etc/rc.bcnu
					echo "$BCNUHOME/etc/bcnud_server start" >>/etc/rc
			fi
			else # hpux10+
				rm /sbin/rc0.d/K*bcnu /sbin/rc1.d/K*bcnu /sbin/rc2.d/K*bcnu /sbin/rc2.d/S*bcnu 2>/dev/null
				ln -s $BCNUHOME/etc/bcnud_server /sbin/rc2.d/S999bcnu
				ln -s $BCNUHOME/etc/bcnud_server /sbin/rc0.d/K10bcnu
				ln -s $BCNUHOME/etc/bcnud_server /sbin/rc1.d/K10bcnu
				ln -s $BCNUHOME/etc/bcnud_server /sbin/rc2.d/K10bcnu
			fi
			;;

	*)		echo "Don't know yet ;)"
			;;
	esac
	
else
		echo "bcnu must be started manually when the system is restarted"
fi

if [ -z "logfiles" ]
then
	printf "Do you want to change permissions on logfiles to allow monitoring? y/n "
	read yorn
else
	yorn="$logfiles"
fi

if [ "y" = "$yorn" ]
	then
		echo "Modifying permission on logfiles - N.B may show up in cops etc"
		for log in $LOGFILES
			do
				if [ -f $log ]
					then
						chmod +r $log
				fi
			done
fi
if [ -z "$delete" ]
then
	printf "Do you want to remove support for non-$BCNUSERVER platforms to save space? y/n "
	read yorn
else
	yorn="$delete"
fi
if [ "y" = "$yorn" ]
	then
		rm $BCNUHOME/platform/*
		echo "Other platforms removed"
fi

echo "Tidying up permissions"
$BCNUHOME/scripts/fix_perms

############################################ Finished ########################
cd $homenow
echo "Client Install Complete"
echo "You may want to check the config files in the $BCNUHOME/etc directory"
echo "To start bcnud run "
echo $BCNUHOME/etc/bcnud_server start

