#!/sbin/sh
# @(#) $Revision: 1.2.212.1 $
# This script converts a DDFA implementation from the old method of
# naming device files (that is, placing them anywhere in the filesystem)
# to the new method of naming device files (that is, placing them only
# in /dev/telnet).  This conversion is suggested at 10.0 and will be
# mandatory later.

# This script does the following:
#
#    1. Kills all ocd and telnetd processes
#
#    2. Removes all device files specified in dedicated ports files
#
#    3. Changes all device file references in dedicated ports files
#
#    4. Removes the DDFA utmp file and the Telnet ports file
#

# Check that dedicated ports file names are specified on command line
if [ $1 ]
then

# Check that there are no duplicate device file names
    rm -f tempbaselist
    for i
    do
        for device in `grep -v '^[	 ]*#' $i | grep -v '^[	 ]*$' | \
                                                   awk '{print $3}'` ; do
            basename $device >> tempbaselist
        done
    done

    sort tempbaselist | uniq -c | sort -n > tempfilelist

    message="0"
    for times in `awk '{print $1}' tempfilelist` ; do
        if [ $times -ne "1" ]
        then
            message="1"
        fi
    done

    if [ $message -eq "1" ]
    then
        echo "The following device file names have more than one reference."
        echo "They are listed below with the number of references for each"
        echo "device file name:"
        awk '/^[	 ]*[2-9][0-9]*[	 ]*/ {print $0}' tempfilelist
        echo "Do you wish to continue and alter the device file names after"
        echo "the conversion takes place? (y/n)"

        read answer

        echo " "
    else
        answer="y"
    fi

    rm -f tempbaselist tempfilelist

    if [ $answer = "y" ]
    then

# Kill all ocd and telnetd processes
        echo "Killing all ocd and telnetd processes"
    
        for pid in `ps -ef | grep -E '(ocd|telnetd)' | grep -v grep | \
                                                       awk '{print $3}'` ; do
            kill -15 $pid
        done

# Remove all device files specified in dedicated ports files
        echo " "
        echo "Removing all device files specified in dedicated ports files"
    
        for i
        do
            echo "    Removing all device files specified in $i"
    
            for device in `grep -v '^[	 ]*#' $i | grep -v '^[	 ]*$' | \
                                                   awk '{print $3}'` ; do
                echo "        Removing $device"
    
                rm -f $device
            done
        done

# Change all device file references in dedicated ports files
        echo " "
        echo "Changing all device file references in dedicated ports files"
    
        for i
        do
            echo "    Changing all device file references in $i"
            echo "    Saving current $i in ${i}.old"
    
            grep -En '(^[	 ]*#|^[	 ]*$)' $i > tempfile1
            grep -Env '(^[	 ]*#|^[	 ]*$)' $i | \
                sed -e's/\([0-9]*:[ \t]*[^ \t][^ \t]*[ \t][ \t]*[^ \t][^ \t]*[ \t][ \t]*\)[^ \t][^ \t]*\//\1\/dev\/telnet\//' > tempfile2
    
            cat tempfile1 tempfile2 | sort -nt':' | sed -e's/^[0-9]*://' > \
                                                    tempfile
    
            mv $i ${i}.old
            mv tempfile $i
        done

        rm -f tempfile tempfile1 tempfile2

# Remove the DDFA utmp file and the Telnet ports file
        rm -f /var/adm/utmp.dfa /var/adm/dpp_login.bin

# Put out final message    
        echo " "
        echo "The DDFA product has been converted for use with the directory"
        echo "/dev/telnet.  The dpp command may now be run to start the DDFA"
        echo "product with the new configuration."
        echo " "
        echo "NOTE: It most likely will be necessary to change the DDFA device"
        echo "      file references used by applications (such as lp,"
        echo "      OpenSpool, and the like) so that they now point to device"
        echo "      files in /dev/telnet."
    fi
else
    echo "No dedicated ports file names on command line"
fi
