#!/bin/sh

moan () {
    echo "*** $@ ***"
}
die () {
    moan "$@"
    rm -f /tmp/gm_devlink1_$$ /tmp/gm_sed_$$
    exit 1
}
syntax () {
    echo Syntax: "$0" "<dest_install_dir>  to install only user-level GM"
    echo Syntax: "$0" \[--ip\] "<dest_install_dir> to install IP driver too"
    echo Syntax: "$0" \[--uninstall\] "<dest_install_dir> to uninstall"
#    echo Syntax: "$0" \[--rc3\] "<dest_install_dir> to create S99myrinet rc3 script"
	die
}
killproc() {            # kill the named process(es)
    pid=`/usr/bin/ps -e |
	 /usr/bin/grep $1 |
	 /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
    [ "$pid" != "" ] && kill $pid
}
	

############
# Argument parsing
############

version=`uname -r`

echo "OS version is $version"

if [ $version -eq "2.5" ]; then
    die GM only works for 2.6 or later.
fi

if [ $version -eq "2.5.1" ]; then
    die GM only works for 2.6 or later.
fi

echo "OS Version $version is OK"

# Verify this script was run from the correct directory
if [ ! -f ./sbin/gm ]; then
    die You must run GM_INSTALL from the directory where it resides 'gm/binary'.
fi

# Determine whether we should install or uninstall

install=1
active=0
while [ "x$1" != x ]; do
    case "$1" in
    	--rc3)
    	    install=3
    	    shift
    	    ;;
    	--ip)
    	    install=2
    	    shift
    	    ;;
    	--uninstall|-u)
    	    install=0
    	    shift
    	    ;;
    	-*)
    	    echo Unrecognized paramenter ""$1"".
	    syntax
    	    ;;
    	*)
	    if [ "x${prefix}" != x ]; then
    	        echo Multiple prefixes specified \(\"${prefix}\" and \"$1\"\).
    	        exit 1
	    fi
	    prefix="$1"
	    shift
    	    ;;
    esac
done

if [ $install -ge 1 ]; then
    msg_prefix="Installing GM driver."
    echo "Installing GM driver."
else
    echo Uninstalling GM.
fi

if [ "x${prefix}" = x ]; then
    echo No destination installation directory specified.
	echo The dest_install_dir is where the GM binaries and driver will be copied.
	echo " "
	echo "To leave the binaries here, just type "
	echo "          ./GM_INSTALL \`pwd\`"
	echo " "
    syntax
else
    echo Installation dir is \""${prefix}".\"
fi



echo "How many Myrinet boards are you installing in this machine? (1,2,3,4)"
read COUNT

if [ $COUNT -le 0 ]; then
    echo "Count '$COUNT' less than 1, please type a number between 1 and 4"
    exit
fi

if [ $COUNT -ge 5 ]; then
    echo "You asked for more boards than the script can handle (max = 4)"
    exit
fi


############
# Fix lomempages
############

if [ x`arch` = xi86pc ]; then
    if `grep "lomempages=130" /etc/system > /dev/null`; then
        /bin/true
    else
        echo Fixing Solaris x86 lomempages problem

        sed -e '/set lomempages/d' /etc/system > /tmp/system1$$
        cat > /tmp/gm_sed_$$ <<-"EOF"
        $a\
        set lomempages=130
EOF
        sed -f /tmp/gm_sed_$$ /tmp/system1$$ > /tmp/system2$$
        cp /etc/system /etc/system.save
        mv /tmp/system2$$ /etc/system
        rm -f /tmp/system1$$ /tmp/system2$$

        echo
        echo 'Added "lomempages=130" to your /etc/system.'
        echo 'Please reboot now so this can take effect, and then'
        echo 'install GM again once the machine has come back up.'
        echo
        exit 1
    fi
fi

############
# Copy new files (or remove old files.) if not an in-place installation.
############

# Copy files iff the install and source directories are different.  Use
# `pwd` in each directory to canonicalize the directory names.

killproc mapper
sleep 2

echo "Removing any existing gm driver."
echo "ifconfig and unplumb myri0 - myri3 just in case"
/sbin/ifconfig myri0 down > /dev/null 2>&1
/sbin/ifconfig myri0 unplumb > /dev/null 2>&1
/sbin/ifconfig myri1 down > /dev/null 2>&1
/sbin/ifconfig myri1 unplumb > /dev/null 2>&1
/sbin/ifconfig myri2 down > /dev/null 2>&1
/sbin/ifconfig myri2 unplumb > /dev/null 2>&1
/sbin/ifconfig myri3 down > /dev/null 2>&1
/sbin/ifconfig myri3 unplumb > /dev/null 2>&1
echo "rem_drv myri and rem_drv gm"
/usr/sbin/rem_drv myri
/usr/sbin/rem_drv gm
/bin/rm -f /dev/gm? /dev/gmp?
/bin/rm -f /dev/myri?
if [ "x`pwd`" != "x`cd ${prefix}; pwd`" ]; then
    if [ ${install} -ge 1 ]; then
        echo Creating directories.
        # echo mkdir "${prefix}"
        mkdir ${prefix} > /dev/null 2>&1
        if [ ! -d "${prefix}" ]; then
            moan Could not create directory "${prefix}."
	    die "(Perhaps you should create it yourself.)"
        fi
        files=`find . -type d -print || die Could not create file list`
        for file in ${files}; do
          # echo mkdir "${prefix}/${file}"
          mkdir ${prefix}/${file} > /dev/null 2>&1
        done
        echo Copying files.
        files=`find . -type f -print || die Could not create file list`
        for file in ${files}; do
	  echo "  "${prefix}/${file}
          cp ${file} ${prefix}/${file} \
          || die Could not create \"${prefix}/${file}.\"
        done
    else    
        echo Removing files.
        files=`find . -type f -print || die Could not create file list`
        for file in ${files}; do
          rm ${prefix}/${file}
        done
        echo Removing directories.
        files=`find . -type d -print || die Could not create file list`
        for file in ${files}; do
          rmdir ${file}
        done
    fi
fi


############
# Modify /etc/devlink.tab
############

echo Modifying /etc/devlink.tab

# Remove out-of-date lines from /etc/devlink.tab
cat > /tmp/gm_sed_$$ <<-"EOF"
	/^.*name=pci10e8,8043[ 	]*.*$/d
	/^.*name=pci14c1,8043[ 	]*.*$/d
	/^.*name=pci103c,106b[ 	]*.*$/d
	/^.*name=gm[ 	]*.*$/d
	/^.*name=myri[ 	]*.*$/d
	/^.*name=mlanai[ 	]*.*$/d
	/^.*name=MYRICOM,mlanai[ 	]*.*$/d
EOF
sed -f /tmp/gm_sed_$$ /etc/devlink.tab > /tmp/gm_devlink1_$$

if [ ${install} -ge 1 ]; then
    # Append lines for PCI to /etc/devlink.tab, if PCI bus present
    devices=`ls -d /devices/pci@* 2> /dev/null`
    if [ "x$devices" != x ]
    then
        cat >> /tmp/gm_devlink1_$$ <<-"EOF"
		type=ddi_pseudo;name=pci10e8,8043	\M0
		type=ddi_pseudo;name=pci14c1,8043	\M0
		type=ddi_pseudo;name=pci103c,106b	\M0
EOF
    fi
  
    # Append lines for SBUS to /etc/devlink.tab, if SBUS present
    devices=`ls -d /devices/sbus@* 2> /dev/null`
    if [ "x$devices" != x ]; then
        cat >> /tmp/gm_devlink1_$$ <<-"EOF"
		type=ddi_pseudo;name=MYRICOM,mlanai	\M0
EOF
    else
    	# Append lines for SBUS to /etc/devlink.tab, if iommu/SBUS present
    	devices=`ls -d /devices/iommu@*/sbus@* 2> /dev/null`
    	if [ "x$devices" != x ]; then
        	cat >> /tmp/gm_devlink1_$$ <<-"EOF"
        	type=ddi_pseudo;name=MYRICOM,mlanai \M0
EOF
    	fi
	fi

    cat >> /tmp/gm_devlink1_$$ <<-"EOF"
		type=ddi_pseudo;name=myri	\M0
EOF
    
fi

# Back up /etc/devlink.tab, unless it's already backed up.
if [ ! -f /etc/devlink.tab.save ]; then
  cp /etc/devlink.tab /etc/devlink.tab.save \
	|| die Could not backup /etc/devlink.tab
fi

# Install the modified /etc/devlink.tab
echo Updating /etc/devlink.tab
cp -f /tmp/gm_devlink1_$$ /etc/devlink.tab \
  || die Could not update /etc/devlink.tab
  devlinks

############
# Install the driver
############

arch=`isainfo -b`
if [ $arch -eq "64" ]; then
    subdir=sparcv9
else
    subdir=
fi 

echo subdir=$subdir

echo Removing old driver files in /kernel/$subdir/drv
rm -f /kernel/drv/$subdir/gm
rm -f /kernel/drv/gm.conf
if [ ${install} -ge 1 ]; then
    echo "Adding the GM (gm) driver"
    ln -s ${prefix}/sbin/gm /kernel/drv/$subdir/gm || die Could not link driver.
    /sbin/sync
    /usr/sbin/add_drv -v -i '"pci10e8,8043" "pci14c1,8043" "pci103c,106b" "MYRICOM,mlanai"' -m '* 0666 bin bin' \
	/kernel/drv/$subdir/gm || die Could not add driver.
    /sbin/sync
fi

rm -f /kernel/drv/$subdir/myri
rm -f /kernel/drv/myri.conf

if [ ${install} -ge 2 ]; then
    echo "Adding the GM (myri) IP driver"
    ln -s ${prefix}/sbin/myri /kernel/drv/$subdir/myri || die Could not link driver.
    ln -s ${prefix}/sbin/myri.conf$COUNT /kernel/drv/myri.conf || die Could not link driver.
    /sbin/sync
    /usr/sbin/add_drv -v -i 'myri' -m '* 0666 bin bin' \
    /kernel/drv/$subdir/myri || die Could not add driver.
    /sbin/sync
fi

############
# Arrange for mapper and IP support to start automatically upon reboot by
# creating ${prefix}/S99myrinet
############

# Make a mapper.args be a link to the right parameters file

#rm -f /kernel/drv/gm_mapper.args
#if [ ${active} -ge 1 ]; then
#    ln -s ${prefix}/sbin/active.args /kernel/drv/gm_mapper.args \
#    || die Could not create mapper.args
#else
#    ln -s ${prefix}/sbin/passive.args /kernel/drv/gm_mapper.args \
#    || die Could not create mmd.parameters
#fi

# Make ${prefix}/S99myrinet

#if [ ${install} -ge 3 ]; then
	echo Removing /tmp/S99myrinet
	rm -f /tmp/S99myrinet
    echo " "
    echo "Creating /tmp/S99myrinet"
    echo "If you want to set up the driver to autoload after a reboot "
    echo "you will need to copy /tmp/S99myrinet to /etc/rc3.d and edit it."
    echo "                cp -p /tmp/S99myrinet /etc/rc3.d/S99myrinet"
	echo " "

    # Hack start of file to include hard-coded prefix using variable
    # substitution.

    cat > /tmp/S99myrinet <<-EOF
	#!/bin/sh
	prefix=${prefix}
EOF

    # Append the rest of the file without variable substitution.

    cat >> /tmp/S99myrinet <<-"EOF"
	#%%%%%%%%%%%%%%%%%%%%%% /etc/rc3.d/S99myrinet %%%%%%%%%%%%%%%%%%%%%%%
	#
	# myrinet.rc: rc script for myrinet 
	#
	#
	
	killproc() {            # kill the named process(es)
	    pid=`/usr/bin/ps -e |
		 /usr/bin/grep $1 |
		 /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	    [ "$pid" != "" ] && kill $pid
	}
	
	case $1 in
	'start')
	
	#echo Starting Myrinet mapping daemon.
	# this causes a load of the GM driver and makes it stick
	modload /kernel/drv/gm
	( cd ${prefix}/bin; ./gm_board_info )
	# this sets the hostname in the GM interface
	( cd ${prefix}/bin; ./gm_setname -h `hostname` )
	#use only one of these commands (if any at all)
	#(cd ${prefix}/sbin; ./mapper ./passive.args ) &
	#(cd ${prefix}/sbin; ./mapper ./active.args ) &

	# Configure Myrinet IP interface
	# Uncomment the commented commands below.
	# Be sure to use the correct hostname hostnumber and subnet mask.
	
    # This must be done to load the myri driver
	# echo "Plumbing Myrinet interface"
	# /sbin/ifconfig myri0 plumb
	
	#HOSTNAME=your-hostname
	#/sbin/ifconfig myri0 $HOSTNAME netmask 255.255.255.0 \
	#       -trailers broadcast $SUBNET.255 up
	
	#/sbin/ifconfig myri0
	
	#/usr/sbin/route add net $SUBNET.0 $SUBNET.$HOSTNUM 0
	
	echo ;;
	
	'stop')
	
	echo ;;
	
	*)      echo "Invalid option supplied to $0"
		    exit 1;;
	esac
	#%%%%%%%%%%%%%%%%%%%%%% /etc/rc3.d/S99myrinet %%%%%%%%%%%%%%%%%%%%%%%
EOF
#fi

chmod a+rx /tmp/S99myrinet

############
# Cleanup
############

rm -f /tmp/gm_devlink1_$$ /tmp/gm_sed_$$
exit 0
