#! /bin/sh

# This script tries to partially install DIPC in your system. The first
# sections do some general tests for possible problems. If every thing was
# OK, the script inputs the install options and attempts to do its job.
 
KERNELPATH=/usr/src/linux

echo
echo This script partially installs the DIPC V1.1 software. 
echo
if test `whoami` != root; then
 echo You should be the root to run this script.
 exit 1
else
 echo Press RETURN to continue, or Control-C to break...
 read
fi
 
#checking to see if dipcd processes are running
ps -ax >/tmp/dipc_check_tasks
grep dipcd /tmp/dipc_check_tasks >/dev/null
if test $? -eq 0; then
 echo 
 echo dipcd processes seem to be running. You could use the \'ps -ax\' command 
 echo to find them \(they are named \'/usr/sbin/dipcd\'\), and then use  
 echo \'kill \<pid\>\' to kill them one by one. If your system has the \'killall\' 
 echo command, you may run \'killall dipcd\'. Then run this script again.
 echo
 rm /tmp/dipc_check_tasks
 exit 1
fi
rm /tmp/dipc_check_tasks

#getting install options
echo 
echo -n "Do you want an automatic installation of DIPC's kernel patch (Y/n)? "
read patchkernel
if [ -z $patchkernel ]; then patchkernel='y'; fi
echo
echo -n "Install DIPC man pages (Y/n)? "
read doman
if [ -z $doman ]; then doman='y'; fi
echo

#see if the user should select a patch version
if [ $patchkernel = 'y' -o $patchkernel = 'Y' ]; then
 echo
 echo Automagic kernel patch for DIPC
 echo "Default kernel path is" $KERNELPATH
 echo -n "Do you want to change this path (e.g. when cross-compiling) (y/N)? "
 read changepath
 if [ -z $changepath ]; then changepath='n'; fi
 if [ $changepath = 'y' -o $changepath = 'Y' ]; then
  echo
  echo -n "Please enter path to kernel source to patch: "
  read KERNELPATH
 fi
 find $KERNELPATH -follow -name \*.orig -print >/tmp/dipc_check_kernel
 grep .orig /tmp/dipc_check_kernel >/dev/null
 if test $? -eq 0; then
  echo Warning. Seems that this kernel has been patched by some package.
  echo This could cause problems for DIPC\'s installation.
  echo -n "Do you want to continue to install DIPC (y/N)? "
  read cont
  if [ -z $cont ]; then cont='n'; fi
  if [ $cont != 'y' -a $cont != 'Y' ]; then
   rm /tmp/dipc_check_kernel
   exit 1
  fi
 fi
 rm /tmp/dipc_check_kernel

 echo
 echo This package patches the  2.1.107  version of the kernel. Patching earlier
 echo kernels may not work.
 echo Use DIPC 1.0 to patch 2.0.x kernels.
 echo
 echo -n "Press Enter to continue..."
 read
 echo
 echo Installing DIPC patch for Linux Kernel 2.1.107;
 gunzip -c kernel/dipc-2.1.107-kernel.diff.gz | (cd $KERNELPATH; patch -l -s -p1 -b .pre_dipc);
 echo
 echo You can find the original kernel files with \'.pre_dipc\' added to them.
fi

echo
if [ ! -e $KERNELPATH/include/linux/dipc.h ]; then
 echo DIPC\'s kernel patch not found. Can\'t continue installing DIPC.
 echo
 exit 1
fi

if [ ! -e $KERNELPATH/include/asm ]; then
 echo It seems that you have not configured your kernel yet. Please do so 
 echo \(make config\), then rerun this script to finish DIPC\'s installation. 
 echo \(Don\'t install DIPC\'s kernel patches the second time\).
 echo 
 exit 1
fi

echo
echo -n "Upgrading from a previous working version of DIPC (y/N)? "
read doup
if [ -z $doup ]; then doup='n'; fi
echo
echo -n "Make DIPC shell tools and install them in /usr/bin (Y/n)? "
read dotools
if [ -z $dotools ]; then dotools='y'; fi
echo
echo -n "Make DIPC X tool and install it in /usr/X11/bin (requires xview) (y/N)? "
read dodipcx
if [ -z $dodipcx ]; then dodipcx='n'; fi
echo
echo -n "Make DIPC Benchmark programs (Y/n)? "
read dobenchmark
if [ -z $dobenchmark ]; then dobenchmark='y'; fi
echo
echo -n "Make DIPC examples (Y/n)? "
read doexams
if [ -z $doexams ]; then doexams='y'; fi
echo

#copying /etc/dipc.* files
cd config-files
echo Copying /etc/dipc.conf and /etc/dipc.allow
cp dipc.conf dipc.allow /etc
echo

#making and installing dipcd
cd ../dipcd
echo making the dipcd program...   
make
echo copying dipcd to /usr/sbin...
cp dipcd /usr/sbin
cd ..
echo

#inserting dipcd invocation in /etc/rc.d/rc.local
grep dipcd /etc/rc.d/rc.local >/dev/null
if test $? -eq 0; then
 echo 
 echo dipcd invocation seems to be present in file /etc/rc.d/rc.local. 
 echo Make sure it is actually executed at boot time.
 echo
else
 echo
 echo adding dipcd invocation to /etc/rc.d/rc.local...
 echo "echo Starting dipcd... " >> /etc/rc.d/rc.local
 echo "/usr/sbin/dipcd 2>/tmp/dipcd.log" >> /etc/rc.d/rc.local
fi
echo

#make the benchmark programs
if test $dobenchmark = 'y' -o $dobenchmark = 'Y'; then
 cd benchmarks
 for prg in syscalls shmem
 do
  echo making benchmarks/$prg...
  cd $prg
  make
  cd ..
  echo
 done
 cd ..
fi
echo


#make the examples
if test $doexams = 'y' -o $doexams = 'Y'; then
 cd examples
 for prg in hello image pi admin game
 do
  echo making examples/$prg...
  cd $prg
  make
  cd ..
  echo
 done
 cd ..
fi
echo

#installing the command line tools
if test $dotools = 'y' -o $dotools = 'Y'; then
 cd tools
 for prg in dipcker dipcshm dipcref
 do
  echo making and installing tools/$prg...
  cd $prg
  make
  cp $prg /usr/bin
  chgrp bin /usr/bin/$prg
  cd ..
  echo
 done
 cd ..
fi
echo

if test $dodipcx = 'y' -o $dodipcx = 'Y'; then
 cd tools
 for prg in dipcx
 do
  echo making and installing tools/$prg...
  cd $prg
  make
  cp $prg /usr/X11/bin
  chgrp bin /usr/X11/bin/$prg
  cd ..
 done
 cd ..
fi
echo

if test $doman = 'y' -o $doman = 'Y'; then
 cp man-pages/man1/*.1.gz /usr/man/man1/
 cp man-pages/man5/*.5.gz /usr/man/man5/
 cp man-pages/man8/*.8.gz /usr/man/man8/
fi

echo PLEASE MANUALLY DO THE FOLLOWING:
if [ $doup != 'y' -a $doup != 'Y' ]; then
 echo *\) Make sure swapping to disk is enabled \(mkswap and swapon\).
 echo *\) Add a user with the name 'dipcd' \(adduser\).
 echo *\) Reconfigure your kernel to make sure System V IPC is present.
fi
echo *\) Edit the file /etc/dipc.conf, and make sure to write the correct 
echo "   addresses for this machine and the referee computer. The dipcx"
echo "   program in tools/dipcx directory could be used for this."
echo *\) If this is the referee computer, edit the file /etc/dipc.allow and
echo "   add the network addresses of the computers in the cluster there."
echo *\) Make and install the new kernel.
echo
echo
echo Read quick.start and docs/installation for more information.
echo
