#! /bin/ksh
#
#  DM-Edit a file to which the process (presumably su'd) has access but
#  the DM does not

#  A directory for temporary files which is not accessible to anyone else:
#
Tempdir=~/.scratch

#  The following is general but slow:
#
DM_owner=$( /bin/ps aux | /usr/bin/egrep ' dm$' | /bin/awk '{print $1}' )

#  For speed just set up your habitual username instead:
#
#DM_owner=jimr

if [ $( /usr/bin/tty ) != "/dev/display" ]
then
    print "Cannot dmsu from $( /usr/bin/tty )"
    exit 1
fi

File=$1

Tempfile=${Tempdir}/${File##*/}

if [ -f ${File} ]
then
	/bin/cp ${File} ${Tempfile}
else
	/usr/bin/touch ${Tempfile}
fi

/usr/apollo/bin/chacl ${DM_owner}=rw ${Tempfile}

if /pm/bin/dmedit ${Tempfile}
then

	if [ -f ${File} ]
	then
		/bin/mv ${File} ${File}.bak
	fi

	/bin/cp ${Tempfile} ${File}

	if [ -f ${File}.bak ]
	then
		/usr/apollo/bin/cpacl ${File}.bak ${File}
	fi
fi

/bin/rm ${Tempfile} ${Tempfile}.bak

/bin/ls -l ${File} ${File}.bak
