#!/bin/csh -f
cat << END

Ultimatte Corporation - AdvantEdge plug-in

 Installation, removal and licensing tool

END

set myid = `id -un`
if($myid != "root") then
    echo "Error: You must be root to run $0"
    exit -1
endif

set uparent = /usr/local/Ultimatte
set uchild  = $uparent/AdvantEdge
set shkDir = /usr/nreal/shake
set tclLib = /usr/local/lib
set dname   = AdvantEdge_dist
set choice = 0

while($choice != 6)
cat << END

Please, make your choice:
1) Install AdvantEdge plug-in only
2) Install AdvantEdge plug-in and Flexlm tools
3) Print the Flexlm host ID
4) Start Ultimatte license daemon
5) Remove  AdvantEdge plug-in and Flexlm tools
6) Quit
END
    echo -n "AdvantEdge > "
    set choice = $<
    if($choice !~ [1-6]) then
	echo " "
	echo Error: $choice is not an option
	continue
    endif


if($choice == 5) then
    echo " "
    echo "Proceed to remove Ultimatte AdvantEdge plug-ins and Flexlm tools ? [y|n]"
    echo -n "AdvantEdge > "
    set deleteit = $<

    if($deleteit == "yes" || $deleteit == "y" || $deleteit == "Y" || $deleteit == "YES") then
	echo " "
	
	echo "Halting the Ultimatte license daemon... "
	$uchild/flexlm/Advantedge_license stop

	if(-e $uparent) then
	    if(-e $uchild/Advantedge.lic) then
		echo Backing up $uchild/Advantedge.lic in ~/Advantedge.lic.bak
		cp $uchild/Advantedge.lic ~/Advantedge.lic.bak
	    endif
	    
	    echo Removing $uparent
	    rm -rf $uparent
	endif

	if(-e $shkDir/include/startup/AdvantEdge.so) then
	    echo Removing $shkDir/include/startup/AdvantEdge.so
	    rm -f $shkDir/include/startup/AdvantEdge.so
	endif

	if(-e $shkDir/include/startup/ui/AdvantEdgeUI.h) then
	    echo Removing $shkDir/include/startup/ui/AdvantEdgeUI.h
	    rm -f $shkDir/include/startup/ui/AdvantEdgeUI.h
	endif

	if(-e $shkDir/icons/AdvantEdge) then
	    echo Removing $shkDir/icons/AdvantEdge
	    rm -rf $shkDir/icons/AdvantEdge
	endif

	if(-e /etc/init.d/Advantedge_license) then
	    echo Removing /etc/init.d/Advantedge_license
	    rm -f /etc/init.d/Advantedge_license
	endif

	if(-e /etc/rc2.d/S52Advantedge_license || -l /etc/rc2.d/S52Advantedge_license) then
	    echo Removing /etc/rc2.d/S52Advantedge_license
	    rm -f /etc/rc2.d/S52Advantedge_license
	endif

	echo " "
    endif
    continue
endif

if($choice < 3) then

    if(! -e $uparent) then
	mkdir $uparent
    endif

    if(! -e $tclLib) then
	mkdir $tclLib
    endif

    if( -e $uchild) then
	rm -rf $uchild
    endif

    mkdir $uchild
    chmod 777 $uchild

    set tarfile = $dname.tar
    set zipfile = $tarfile.gz

    if( -e  $zipfile) then
	gunzip $zipfile
    endif

    if( -e $tarfile) then
	tar xvof $tarfile
	rm -rf $tarfile
	echo " "
    endif

    if( -e $dname) then

	cd $dname

#if(! -e $shkDir/Ultimatte) then
#mkdir $shkDir/Ultimatte
#endif
	if( -e $tclLib) then
	    echo Copying tcl8.4 to $tclLib
	    cp -r tcl8.4 $tclLib
	    echo Copying tk8.4 to $tclLib
	    cp -r tk8.4 $tclLib
	endif


	mkdir $uchild/include
	mkdir $uchild/icons
	mkdir $uchild/include/startup
	mkdir $uchild/include/startup/ui

	foreach idir ($shkDir $uchild)

	    set src  = ./AdvantEdge.so
	    set dst1 = $idir/include/startup
	    echo Copying $src to $dst1 ...
	    cp $src $dst1

	    set src  = ./AdvantEdgeUI.h
	    set dst1 = $idir/include/startup/ui
	    echo Copying $src to $dst1 ...
	    cp $src $dst1

	    set src  = ./icons/fx
	    set dst1 = $idir/icons
	    echo Copying $src to $dst1 ...
	    cp -r $src $dst1

	    set src  = ./icons/fxAlt
	    set dst1 = $idir/icons
	    echo Copying $src to $dst1 ...
	    cp -r $src $dst1

	    set src  = ./icons/AdvantEdge
	    set dst1 = $idir/icons
	    echo Copying $src to $dst1 ...
	    cp -r $src $dst1

	end

	cd ..

    endif

endif

if($choice == 3) then
    echo " "
    if(-e $uchild/flexlm/lmutil) then
	$uchild/flexlm/lmutil lmhostid
    else
	echo "Error: You need to run option 2) first, in order to use this tool"
    endif
    echo " "
    continue
endif

#
#
#

if($choice < 3) then
    set tempFile = ./tmpAdvantEdgeDisp
    echo -n > $tempFile
    set dispFile = /usr/lib/X11/xdm/Xservers
    set dispch = ":0 secure /usr/bin/X11/X -nobitscale -c -class TrueColor -depth 24 -solidroot sgilightblue -cursorFG red -cursorBG white"
    set tmp = `wc -l $dispFile`
    set nlines = $tmp[1]

    set everChanged = 0;
    set i = 1
    while($i <= $nlines)
	set changeIt = 0
	set line = (`head -$i $dispFile|tail -1`)
	if($line[1] == ":0") then
	    set j = 1
	    while($j <= $#line)
		if(X$line[$j] == X"-pseudomap") then
		    set changeIt = 1
		endif
		@ j++
	    end
	    if($changeIt == 1) then
		set nline = "# $line"
		echo "# The line below has been commented out by the installation of Ultimatte AdvantEdge" >> $tempFile
		echo $nline >> $tempFile
		echo $dispch >> $tempFile
		set everChanged = 1;
	    endif
	endif

	@ i++
    end

    if($everChanged == 1) then
	mv $dispFile ${dispFile}.old
	mv $tempFile $dispFile
    endif

    if(-e $tempFile) rm -f $tempFile
endif

#
#
#

# prompt the user for license type

if($choice == 2) then
    set lictype = 0
    while($lictype !~ [1-2])
	echo " "
	echo "Please choose your license type:"
	echo "1) Nodelock"
	echo "2) License Server"
	echo -n "AdvantEdge > "
	set lictype = $<
	echo " "
	if($lictype !~ [1-2]) then
	    echo " "
	    echo Error: $lictype is not an option
	    continue
	endif
    end


    if(-e $uchild/flexlm) then
	rm -rf $uchild/flexlm
    endif

    set src  = ./$dname/flexlm
    set dst1 = $uchild/flexlm
    echo Copying $src to $dst1 ...
    cp -r $src $dst1
    chmod 777 $dst1

    if($lictype == 2) then

	rm -f /etc/init.d/Advantedge_license
	echo Copying ./$dname/flexlm/Advantedge_license in /etc/init.d
	cp ./$dname/flexlm/Advantedge_license /etc/init.d
	rm -f /etc/rc2.d/S52Advantedge_license
	echo Linking /etc/init.d/Advantedge_license to /etc/rc2.d/S52Advantedge_license
	ln -s /etc/init.d/Advantedge_license /etc/rc2.d/S52Advantedge_license

    endif
    continue
endif

if($choice == 4) then

    if(! -e $uchild/Advantedge.lic) then
	echo " "
	echo "Error: You need $uchild/Advantedge.lic in order to use this tool"
	echo " "
	continue
    endif

    echo " "
    echo "Would you like to start the Ultimatte license daemon now? [y|n]"
    echo -n "AdvantEdge > "
    set runit = $<

    if($runit == "yes" || $runit == "y" || $runit == "Y" || $runit == "YES") then
	if(-e $uchild/flexlm/Advantedge_license) then
	    $uchild/flexlm/Advantedge_license start
	    echo " "
	else
	    echo " "
	    echo "Error: You need to run option 2) first, in order to use this tool"
	endif
    endif
    continue
endif

end
