#!/bin/sh

#
# Sensiva Simple Install Script.  Execute in the same directory
#  where the files where untar'd.  e.g.
#   $ cd /tmp
#   $ tar -xzf sensiva-pr1.tgz
#   $ cd sensiva-pr1
#   $ ./INSTALL
#
# This script will install sensiva into the directory
# supplied as an argument, or /usr/lib/X11/sensiva if none
# is supplied.

if [ "$EUID" != "0" ]; then
 echo "You must be superuser to install sensiva."
 exit
fi

SENSIVA=/usr/lib/X11/sensiva

if [ "$1" != "" ]; then
 SENSIVA=$1
fi

#
# create the installation directories.
# Users must be able to write files in these directories
# to add/change symbols and plugins.
# The symbols and plugin files themselves may be write-protected.
#

echo "Creating " $SENSIVA " Install Directories "

mkdir -pm 0777 $SENSIVA/plugins
mkdir -pm 0777 $SENSIVA/symbols
mkdir -pm 0777 $SENSIVA/sounds
mkdir -pm 0777 $SENSIVA/options
mkdir -pm 0755 $SENSIVA/bin
mkdir -pm 0755 $SENSIVA/help

#
# copy the files extracted from the current directory
# into the installation directory
#

echo "Copying Files into " $SENSIVA

# delete anything already there

rm -f $SENSIVA/plugins/*
rm -f $SENSIVA/symbols/*
rm -f $SENSIVA/options/*
rm -f $SENSIVA/sounds/*
rm -f $SENSIVA/help/*
rm -f $SENSIVA/bin/*

cp -af plugins/* $SENSIVA/plugins
cp -af symbols/* $SENSIVA/symbols
cp -af options/* $SENSIVA/options
cp -af sounds/*  $SENSIVA/sounds
cp -af help/*    $SENSIVA/help
cp -af bin/*     $SENSIVA/bin

# 
# now find and modify X startup scripts to run sensiva
# when X starts up.  There are many ways to do this.
# We pick the earliest point in X initialization so it
# will be most likely to succeed.  If you've done
# user X startup customizations, you should be able to read this
# code and do it your own way..
#

XINITRC=$HOME/.xinitrc

if [ -f $HOME/.xinitrc ]; then
 XINITRC=$HOME/.xinitrc
elif [ -f /etc/X11/xinit/xinitrc ]; then
 XINITRC=/etc/X11/xinit/xinitrc
elif [ -f /usr/lib/X11/xinit/xinitrc ]; then
 XINITRC=/usr/lib/X11/xinit/xinitrc
elif [ -f /usr/X11R6/lib/X11/xinit/xinitrc ]; then
 XINITRC=/usr/X11R6/lib/X11/xinit/xinitrc
else
 echo "No xinitrc file found!"
 echo "See INSTALL script to determine how to startup sensiva automatically."
 exit
fi

echo "Updating " $XINITRC " to run Sensiva at X Startup "

SENSIVABIN=$SENSIVA/bin/sensiva

if [ -f $XINITRC ]; then

   if [ ! -f $XINITRC.b4sensiva ]; then

      echo "#!/bin/sh" > /tmp/sensivaxinitrc
      echo "" >> /tmp/sensivaxinitrc
      echo $SENSIVABIN " -p " $SENSIVA " & " >> /tmp/sensivaxinitrc
      echo "" >> /tmp/sensivaxinitrc
      cat $XINITRC >> /tmp/sensivaxinitrc
      mv -f $XINITRC $XINITRC.b4sensiva
      mv -f /tmp/sensivaxinitrc $XINITRC
   else
    echo "Sensiva already installed into X Startup "
   fi
   
fi

#
# Create a hardcoded /usr/bin/X11/sensiva script that invokes sensiva
# passing the installation directory
#
mkdir -pm 0775 /usr/bin/X11
echo "#!/bin/sh" > /usr/bin/X11/sensiva
echo "" >> /usr/bin/X11/sensiva
echo  $SENSIVABIN " -p " $SENSIVA " & " >> /usr/bin/X11/sensiva
chmod 755 /usr/bin/X11/sensiva

echo "Sensiva Successfully Installed and will run at X Startup "

echo -n "Do you want to startup Sensiva now (yes/no) "

read ANSWER

if [ "$ANSWER" = "yes" ]; then
    $SENSIVABIN -p $SENSIVA &
fi

