#!/bin/sh
#
# Retrospect Client Install script
# 6/2008 
# 
# (C) 2010 Sonic Solutions. Portions (C) 1989-2010 EMC Corporation. All rights reserved.
# www.retrospect.com
# 
#  Get the system architecture
#
ARCH=`uname`
CLIENTDIR=/usr/local/dantz/client
STATEFILE=/var/log/retroclient.state
EXCLUDEFILE=/etc/retroclient.excludes

#defaults for suse
INITD=/etc/init.d
RCD=rc.d/

ISROOT="Yes"
if [ $ARCH != "SunOS" ]; then
    if [ "$UID" -ne 0 ] || [ ! -d "/usr/local" ] || [ ! -f ./RCL.tar ]; then
        ISROOT="No"
    fi
else
    MYNAME=`/usr/ucb/whoami`
    if [ $MYNAME != "root" ]  || [ ! -d "/usr/local" ] || [ ! -f ./RCL.tar ]; then 
        ISROOT="No"
    else
        LD_LIBRARY_PATH="/usr/local/lib:/usr/openwin/lib"
        export LD_LIBRARY_PATH
    fi
fi

if [ $ISROOT != "Yes" ]; then
    echo
    echo "Sorry, installation requires that you be logged on as root,"
    echo "that a /usr/local directory already exist, and that "
    echo "the RCL.tar archive is in the current directory."
    exit 0
fi

#if we're on redhat, these are the correct init.d and rc.d locations
[ -f /etc/redhat-release ] && INITD=/etc/rc.d/init.d
[ -f /etc/redhat-release ] && RCD=rc.d/

UPGRADE=""
if [ $ARCH != "SunOS" ]; then
    if [ -x /sbin/pidof ] && [ "$(/sbin/pidof retroclient)" ] || [ "$(ps -e | grep retroclient)" ]; then
        UPGRADE=TRUE
    fi
else
    if [ $ARCH = "SunOS" ]; then
        RUNNINGCOUNT=`/usr/bin/ps -ef | grep retroclient | /usr/bin/grep -v grep | /usr/bin/grep -c retro`
        if [ $RUNNINGCOUNT -gt 0 ]; then
            UPGRADE=TRUE
        fi
    fi
fi
echo
if [ $UPGRADE ]; then
    if [ $ARCH = "SunOS" ]; then
		/usr/ucb/echo -n "Update current install of the Retrospect Client? (y/n): "
	else
		echo -n "Install Retrospect Client? (y/n): "
	fi
else
    if [ $ARCH = "SunOS" ]; then
		/usr/ucb/echo -n "Install Retrospect Client? (y/n): "
	else
		echo -n "Install Retrospect Client? (y/n): "
	fi
fi
read X
if [ "$X" != "y" ] && [ "$X" != "Y" ]; then
	echo "Installation canceled."
	exit 0
fi
if [ $UPGRADE ]; then
	$CLIENTDIR/retrocpl -stop > /dev/null
fi
if [ ! -d "/usr/local/dantz" ]; then
    if [ $ARCH != "SunOS" ]; then
        /bin/mkdir /usr/local/dantz
    else
        /usr/bin/mkdir /usr/local/dantz
    fi
fi
if [ ! -d "$CLIENTDIR" ]; then
    if [ $ARCH != "SunOS" ]; then
        /bin/mkdir $CLIENTDIR 
    else
        /usr/bin/mkdir $CLIENTDIR
    fi
fi
tar -xf ./RCL.tar
if [ $ARCH != "SunOS" ]; then
    /bin/mv -f ./retroclient ./retrocpl ./retroeventhandler ./retrospect.jar ./libRetrospectIO.so \
		./RetroClient.sh $CLIENTDIR 
else
    /usr/bin/mv -f ./retroclient ./retrocpl ./retroeventhandler ./retrospect.jar ./libRetrospectIO.so \
		./RetroClient.sh $CLIENTDIR 
fi
if [ $ARCH != "SunOS" ]; then
	/bin/mv -f ./rcl $INITD
	ln -sf $INITD/rcl $CLIENTDIR/rcl 
else
    if [ ! -f "/etc/rc3.d/S99retroclient" ]; then
	    /usr/bin/mv -f ./rcl /etc/rc3.d/S99retroclient
	    ln -sf /etc/rc3.d/S99retroclient $CLIENTDIR/rcl 
    else
	    /usr/bin/mv -f ./rcl /etc/rc3.d/S99retroclient
		if [ ! -x $CLIENTDIR/rcl ]; then
			ln -sf /etc/rc3.d/S99retroclient $CLIENTDIR/rcl 
		else
			rm -f $CLIENTDIR/rcl
			ln -sf /etc/rc3.d/S99retroclient $CLIENTDIR/rcl
		fi
    fi
fi
if [ ! -f $EXCLUDEFILE ]; then
    /bin/mv -f ./retroclient.excludes /etc
else
    /bin/rm -f ./retroclient.excludes
fi
if [ ! -f $STATEFILE ]; then
    $CLIENTDIR/retroclient -setpass
fi

#
# Install man and info pages
#
if [ ! -d "/usr/local/man/man1" ]; then
    if [ $ARCH != "SunOS" ]; then
        /bin/mkdir -p /usr/local/man/man1
    else
        /usr/bin/mkdir -p /usr/local/man/man1
	fi
fi
/bin/mv -f ./retroclient.1.gz ./retrocpl.1.gz /usr/local/man/man1
if [ -f ./retroclient.1 ]; then
	/bin/mv -f ./retroclient.1 ./retrocpl.1 /usr/local/man/man1
fi
if [ -d "/usr/man/man1" ]; then
	cp -f /usr/local/man/man1/retroclient.* /usr/man/man1
	cp -f /usr/local/man/man1/retrocpl.* /usr/man/man1
fi

# Solaris doesn't have info pages...
if [ $ARCH != "SunOS" ]; then
	if [ ! -d "/usr/share/info" ]; then
        /bin/mkdir -p /usr/share/info
	fi
	/bin/mv -f ./retroclient.info.gz /usr/share/info
	if  [ -L /usr/share/info/dir ]; then
		INFODIRFILE=/etc/info-dir
	else
		INFODIRFILE=/usr/share/info/dir
	fi
cat<< EOF >> $INFODIRFILE

Retrospect Client 
* retroclient: (retroclient).					Retrospect Client.
* retrocpl: (retroclient)retrocpl.				Retrospect Client Control Panel.
EOF
fi

#
#  Set up the environment variables
#
#  Bash/sh first
echo ""
if [ $ARCH != "SunOS" ]; then
	echo -n "Adding RETROSPECT_HOME to system profile and login scripts..."
else
	/usr/ucb/echo -n "Adding RETROSPECT_HOME to system profile and login scripts..."
fi

if [ -f /etc/profile ]
then
	ISTHERE=`grep RETROSPECT_HOME /etc/profile`
	if [ ! -z "$ISTHERE" ]
	then
		cp /etc/profile /etc/profile.old
		sed -e "/RETROSPECT_HOME/d" < /etc/profile.old > /etc/profile
	fi
	echo "RETROSPECT_HOME=$CLIENTDIR" >> /etc/profile
	echo "export RETROSPECT_HOME" >> /etc/profile
fi
#
#  csh now
#
if [ -f /etc/.login ]
then
	ISTHERE=`grep RETROSPECT_HOME /etc/.login`
	if [ ! -z "$ISTHERE" ]
	then
		cp /etc/.login /etc/.login.old
		sed -e "/RETROSPECT_HOME/d" < /etc/.login.old > /etc/.login
	fi
	echo "setenv RETROSPECT_HOME $CLIENTDIR" >> /etc/.login
fi
echo "Done!"
echo ""
echo "Before attempting to use any of the GUI components of the Retrospect Client,"
echo "you must have both the RETROSPECT_HOME and DISPLAY variables set.  The"
echo "RETROSPECT_HOME variable should point to $CLIENTDIR.  We have"
echo "modified your /etc/profile and /etc/.login files to set this value for future"
echo "logins.  The retrospect client launched as part of this installation process"
echo "will also have this variable set.  However, if you kill that client and"
echo "restart it without logging out, you will need to set these variables manually."
echo ""

echo "Starting client as daemon... "
RETROSPECT_HOME="$CLIENTDIR"
export RETROSPECT_HOME
$CLIENTDIR/rcl start
echo
if [ $ARCH != "SunOS" ]; then
    if [ ! -x /sbin/pidof ] && [ "$(/sbin/pidof retroclient)" ] || [ -z "$(ps -e | grep retroclient)" ]; then
        echo "Installation was successful, but Retrospect Client for Linux could not "
        echo "be started. If this is an upgrade, please wait 5 minutes, then start "
        echo "the client manually by running  $ $CLIENTDIR/rcl start"
    else
        echo "Retrospect Client for Linux started successfully!"
    fi
else
    RUNNINGCOUNT=`/usr/bin/ps -ef | grep retroclient | /usr/bin/grep -v grep | /usr/bin/grep -c retro`
    if [ $RUNNINGCOUNT -gt 0 ]; then
        echo "Retrospect Client for Solaris started successfully!"
    else
        echo "Installation was successful, but Retrospect Client for Solaris could not "
        echo "be started. If this is an upgrade, please wait 5 minutes, then start "
        echo "the client manually by running  $ $CLIENTDIR/rcl start"
    fi
fi
if [ $UPGRADE ]; then
    echo "Upgrade completed successfully"
else
    if [ $ARCH != "SunOS" ]; then
        if [ -d "/etc/${RCD}rc2.d" ] && [ ! -f "/etc/${RCD}rc2.d/S99retroclient" ]; then
            echo "Your system appears to be compatible with the startup system used "
            echo "by Red Hat and S.u.S.E. Would you like to create symbolic links "
            echo -n "to automatically start Retrospect Client for Linux? (y/n): "
            read X
            if [ "$X" = "y" ]; then
                for Y in 2 3 5; do
                    ln -s $CLIENTDIR/rcl /etc/${RCD}rc$Y.d/S99retroclient
                done
            fi
        elif [ ! -d "/etc/${RCD}rc2.d" ]; then
            echo "If your system uses an rc.local script, you should be able to start "
            echo "Retrospect Client for Linux automatically by adding the line "
            echo "'$CLIENTDIR/rcl start'. Otherwise, consult your system "
            echo "documentation for the best method."
        fi
    else
        if [ -d "/etc/rc3.d" ] && [ ! -f "/etc/rc3.d/S99retroclient" ]; then
            echo "Would you like to create symbolic links to automatically start "
            /usr/ucb/echo -n "Retrospect Client for Solaris? (y/n): "
            read X
            if [ "$X" = "y" ]; then
                for Y in 2 3 5; do
                    ln -s $CLIENTDIR/rcl /etc/rc.d/rc$Y.d/S99retroclient
                done
            fi
        elif [ ! -d "/etc/rc3.d" ]; then
            echo "You should install a startup script in /etc/rc3.d named S99retroclient"
            echo "to start the Retrospect Client for Solaris automatically when you"
            echo "boot your system.  See your system documentation for more information."
        else
            echo "Installation completed successfully"
        fi
    fi
fi
echo
exit 0
