#!/bin/sh

MyOS=`uname`
if [ "$MyOS" = "SunOS" ]; then
    id=/usr/xpg4/bin/id
else
# Good for AIX and HP
    id=id
fi

if [ "`$id -ru`" != "0" ]; then
    echo You have no permission to execute this script
    exit
fi

new_admin=$1


if [ -z "$1" ]; then
echo Please enter the user name of the new MSC administrator: | tr -d '\012'
read new_admin
#echo Usage: $0 \<new_administrator_name\>
fi

\ypmatch $new_admin passwd > /dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo
echo Invalid username - \"$new_admin\". Run \""ypcat passwd | sed 's=:.*$==g'"\" for valid username list
echo
echo Usage: mwadm chadm \[ \<new_administrator_name\> \]
exit
fi

echo Please re-enter user name: | tr -d '\012'
read new_admin_next

if [ "$new_admin_next" != "$new_admin" ] ; then
echo
echo Username missmach, try again.
echo 
echo Usage: mwadm chadm \[ \<new_administrator_name\> \]
exit
fi


# First, stop core services in order to change their administrator
mwadm stop -f

\rm /tmp/mainwin.conf 2> /dev/null
cat /etc/mainwin.conf | sed -e "s/CORE_ADMIN=.*/CORE_ADMIN=$new_admin/g" > /tmp/mainwin.conf
mv /tmp/mainwin.conf /etc/mainwin.conf
chmod 644 /etc/mainwin.conf

core_path=`grep CORE_PATH /etc/mainwin.conf | cut -d= -f2`
core_data=`grep CORE_DATA /etc/mainwin.conf | cut -d= -f2`
echo $core_data

chown -R $new_admin $core_data
chown root $core_data/run_with_uid
chmod 04555 $core_data/run_with_uid

\rm -rf /tmp/mw 2> /dev/null

MWHOME=$core_path/mw
. $MWHOME/setmwruntime


echo The new MSC administrator is $new_admin
echo Restarting core services...
mwadm start

#changing registry ownership 
$MWHOME/bin/tools/core_hook3.sh $new_admin $core_path

echo Operation completed successfully.

