#!/bin/csh -f
# Generated and maintained by Olaf Mueller

set source			= "$1"
set target			= "$2"

set bindir			= "/usr/local/bin"
set BINS			= "amlsyntax dial ps2fps snd2zsnd zsnd2snd zsndplay zsnd2mdm"
set	gettytab		= "/private/etc/gettytab"
set gettycomment	= "# Voice/Fax/Data dialup with OlafAM"
set	rclocal			= "/private/etc/rc.local"
set rclocalcomment	= "# Starting OM daemons from $2/OlafAM.app/lib"



/usr/etc/chown -R root.wheel $target/OlafAM.app


# make soft links of bins in /usr/local/bin
if ( ! -d $bindir) then
	mkdirs $bindir
	if ( $status != 0 ) then
		echo FAILED. Cannot create $bindir
		exit 1
	endif
endif

foreach bin ($BINS)
	if ( -e $bindir/$bin) then
		set fileresult = `/bin/file $bindir/$bin`
		set filetype = `echo $fileresult | /bin/awk '{print $2}'`
		if ( $filetype == "symbolic" ) then
			set linkto = `echo $fileresult | /bin/awk '{print $5}'`
			if ( $linkto != "$target/OlafAM.app/bin/$bin" ) then
				mv $bindir/$bin $bindir/$bin.old
				ln -s $target/OlafAM.app/bin/$bin $bindir
			endif
		else
			mv $bindir/$bin $bindir/$bin.old
			ln -s $target/OlafAM.app/bin/$bin $bindir
		endif
	else
		ln -s $target/OlafAM.app/bin/$bin $bindir
	endif
end


# change some modes
/bin/chmod 4755 $target/OlafAM.app/bin/dial
/bin/chmod 4755 $target/OlafAM.app/bin/zsnd2mdm


# insert the dialup in gettytab
@ ii = `cat $gettytab | fgrep -c "$gettycomment"`
if ( $ii == 0 ) then
	echo "" >> $gettytab
	echo $gettycomment >> $gettytab
	echo "VFDmach.19200:ap:p8:im=\r\n\r\nNeXT / Voice/Fax/Data answering machine (%h) (%t)\r\n\r\r\n\r:sp#19200:" >> $gettytab
	echo "VFDmach.38400:ap:p8:im=\r\n\r\nNeXT / Voice/Fax/Data answering machine (%h) (%t)\r\n\r\r\n\r:sp#38400:" >> $gettytab
	echo "VFDmach.57600:ap:p8:im=\r\n\r\nNeXT / Voice/Fax/Data answering machine (%h) (%t)\r\n\r\r\n\r:sp#57600:" >> $gettytab
endif


# insert the startup OM daemon
@ ii = `cat $rclocal | fgrep -c "$rclocalcomment"`
if ( $ii == 0 ) then
	echo "" >> $rclocal
	echo $rclocalcomment >> $rclocal
	echo "if [ -f $2/OlafAM.app/lib/startomd ]; then" >> $rclocal
	echo "	$2/OlafAM.app/lib/startomd" >> $rclocal
	echo "fi" >> $rclocal
endif


# shutdown OM daemon
set pid = `ps -ax | fgrep OMDaemon | fgrep -v fgrep`
if ($status == 0) then
	set pid = `echo $pid | awk '{print $1}'`
	kill -INT $pid
endif


# restarting am
set pid = `ps -ax | fgrep \(am\) | fgrep -v fgrep`
if ($status == 0) then
	set pid = `echo $pid | awk '{print $1}'`
	kill -INT $pid
endif


# adjust previous versions of OlafAM
set cfgname = OM_`hostname`_fax.cfg
if ( -f $2/OlafAM.app/lib/$cfgname) then
	mv $2/OlafAM.app/lib/$cfgname /private/etc
endif


echo OK
exit 0
