#!/usr/bin/sh
#****************************************************************************
#*                                                                          *
#*              Copyright (C) 1995; Agilent Technologies                    *
#*                                                                          *
#****************************************************************************
#*                                                                          *
#*  System Name     : acceSS7                                               *
#*  File Type       : Utility Shell Script                                  *
#*  File name       : dist_passwd                                           *
#*  Date Written    : 1st March 1995                                        *
#*                                                                          *
#****************************************************************************
#
# If the Platform profile script does not exist, then the Platform
# is absent or corrupt.  No point receiving endless mail from cron,
# so just exit with success.

[[ ! -r /opt/platform7/lbin/p7profile ]] && exit 0

# Source in p7profile script to set up UX10 pathname environment variables

. /opt/platform7/lbin/p7profile

# Only distribute if the local server is on-line

if [[ ! -r $P7_SVRSTATE_FILE ]]
then
    echo $0: "$P7_SVRSTATE_FILE does not exist"
    exit 1
fi

read STATE < $P7_SVRSTATE_FILE

[[ $STATE != +(ONLINE|ONLINE_STDALONE) ]] && exit 1


#
# If local passwd management is in effect then no need to distribute so
# return.

AUTH_LOCAL_PSWD_MGMT=$($P7_INST_DIR/lbin/p7resource AuthLocalPswdMgmt)
if [ $? != 0 ]
then
    echo $0: "Unable to read AuthLocalPswdMgmt resource parameter"
    exit 1
elif [[ $AUTH_LOCAL_PSWD_MGMT != "0" ]]
then
    exit 0
fi

# lock access to password file

typeset -i count=0
while [[ -r /etc/ptmp ]]
do
    (( count = count + 1 ))
    if [[ $count -gt 5 ]]
    then
        echo $0: "/etc/passwd locked"
        exit 2
    fi
    sleep 2
done
touch /etc/ptmp

# lock dist_passwd flag/data file
if [[ -r /etc/passwd.dtmp ]]
then
    echo $0: "dist_passwd already running"
    rm -f /etc/ptmp
    exit 2
fi    
cp /etc/passwd /etc/passwd.dtmp

# Unlock access to password file
rm -f /etc/ptmp

# Locked, get list of workstations in the acceSS7 database (not comms database)

WSLIST=`$P7_INST_DIR/lbin/list_processors -w`

# Get server's (local machine) hostname

LCHOST=`hostname`

# Loop thru each workstation

for i in $WSLIST
do

# Copy the server's password file to the workstation.
# (unless it is the co-located workstation which resides on the server).
# If remsh produces any output, then prefix it with the workstation
# hostname and write to stdout (to be seen in cron mail on server).

	if [ $i != $LCHOST ]
	then
	    output=$(remsh $i $P7_INST_DIR/lbin/a7cppasswd < /dev/null 2>&1)
	    [[ -n "$output" ]] && echo "Error on $i: $output"
	fi
done

# Remove dist_passwd tmp file
rm -f /etc/passwd.dtmp
