#!/bin/csh -f
# Installation script for OCAAS CDROM.

# must be root
if (`whoami` != "root") then
    echo You must be root to run this.
    exit 1
endif

# intro
if ( -x /usr/bin/clear ) clear
echo "                This is the OCAAS installation script."

# find the cdrom root by looking for the version signature
set vfn = telescope/VERSION
if (-e /mnt/cdrom/$vfn) set cdrom = /mnt/cdrom
if (-e /cdrom/$vfn) set cdrom = /cdrom
if (-e $0:h/$vfn) set cdrom = $0:h
if (-e $1/$vfn) set cdrom = $1
if (! $?cdrom) then
    echo can not find path to cdrom -- type as argument to this script and rerun
    exit 1
endif

# cd to cdrom base from now on
cd $cdrom

# get version
set version = `cat $vfn`
set pre = "pre-$version"

# confirm intent
echo ""
echo -n "Proceed to install OCAAS Version $version? [y] (y/n) "
if ("$<" == "n") then
    echo installation cancelled.
    exit 0
endif

# confirm licensed
if ( -x /usr/bin/clear ) clear
cat COPYRIGHT
if ($status) then
    echo Can not find COPYRIGHT
    exit 1
endif
echo ""
echo -n "Have you read the COPYRIGHT above and executed a license agreement? [n] (y/n) "
if ("$<" != "y") then
    echo installation not authorized.
    exit 0
endif

# ask installation directory
if ( -x /usr/bin/clear ) clear
set insdir = /usr/local/telescope
echo -n "Base directory: [$insdir] "
set ans = "$<"
if ("$ans" != "") set insdir = $ans

# see if a gsc is still good
set gsc = $insdir/archive/catalogs/gsc
if (-d $gsc/s3730 && -d $gsc/n1500) then
    set gscsave = $insdir/.gsc	# hide name from * when clean insdir
    mv $gsc $gscsave	# hide from *
endif

# preserve or clean out telhome
if (-e $insdir) then
    # do not disturb $insdir itself in case it is a link or something
    echo ""
    echo -n "preserve the existing $insdir? [y] (y/n) "
    set ans = "$<"
    if ($ans == 'n') then
	echo cleaning out $insdir ..
	touch $insdir/x.x
	rm -fr $insdir/*
    else
	set predir = $insdir/telescope.$pre
	echo saving $insdir in $predir
	set tmppre = $insdir/.x.x.$pre	# * won't see this
	rm -fr $tmppre
	mkdir -p $tmppre
	mv $insdir/* $tmppre
	if ($status) then
	    echo can not save $insdir
	    exit 1
	endif
	mv $tmppre $predir
    endif
else
    echo creating $insdir ..
    mkdir -p $insdir
    if ($status) then
	echo can not create $insdir
	exit 1
    endif
endif

# check for enough room there
set telneeds = `du -s $cdrom/telescope | awk '{print $1}'`
if ($?gscsave) then
    set needfree = $telneeds
else
    @ needfree = $telneeds + 160000
endif
set free = `df -P $insdir | awk '! /Filesystem/ {print $4;}'`
if ($free < $needfree) then
    echo need at least ${needfree}MB free but only found ${free}MB
    exit 1
endif

# check for ocaas login and group.
# give option to preserve if find, else create new.
echo ""
grep -q '^ocaas:' /etc/passwd
if ($status) then
    echo "creating new ocaas login and group"
    set ohome = /home/ocaas
    /usr/sbin/useradd -M -d $ohome -s /bin/tcsh ocaas
    echo enter new ocaas login password now
    passwd ocaas
else
    echo user ocaas already exists.
    set ohome = `awk -F: '/^ocaas:/{print $6}' /etc/passwd`
    echo -n "clean out the existing $ohome? [n] (y/n) "
    set ans = "$<"
    if ($ans == 'y') then
	touch $ohome/x.x
	rm -fr $ohome/*
    endif
endif

# let's do it
set clog = "/tmp/OCAAS.$$"
rm -f $clog
echo ""
echo No more questions ..
echo ""
echo Loading new software .. each file is listed in $clog
onintr kp

# copy stuff into $ohome and fix up TELHOME in .cshrc
if ( ! -e $ohome ) mkdir $ohome
(cd home; find . -print | cpio -pdvu $ohome >>& $clog)
set fn = $ohome/.cshrc
set tmp = $fn.tmp
cp $fn $tmp
sed -e 's#/usr/local/telescope#'$insdir'#' < $tmp > $fn
rm $tmp
chown -R ocaas $ohome
chgrp -R ocaas $ohome
chmod -R u+w $ohome $ohome/.[a-z]*

# copy in basic stuff, with a simple progress indicator
( cd telescope; find . -print | cpio -pdv $insdir >>& $clog & )
set lastfreenow = 0
while (1)
    set freenow = `df -P $insdir | awk '! /Filesystem/ {print $4;}'`
    @ new = $lastfreenow - $freenow
    if ($new == 0) break
    set lastfreenow = $freenow
    @ pd = ( ( $free - $freenow ) * 100 ) / $needfree
    if ($pd < 100) /bin/echo -ne "${pd}%\r"
    sleep 5
end

# copy or restore gsc
rm -fr $gsc
if ($?gscsave) then
    mv $gscsave $gsc
else
    ( find gsc -print | cpio -pdv $gsc:h >>& $clog & )
    set lastfreenow = 0
    while (1)
	set freenow = `df -P $insdir | awk '! /Filesystem/ {print $4;}'`
	@ new = $lastfreenow - $freenow
	if ($new == 0) break
	set lastfreenow = $freenow
	@ pd = ( ( $free - $freenow ) * 100 ) / $needfree
	if ($pd < 100) /bin/echo -ne "${pd}%\r"
	sleep 5
    end
endif

# fifos don't come across CDROM
echo making fifos
mkfifo -m 0660 $insdir/comm/xephem_in_fifo
mkfifo -m 0660 $insdir/comm/xephem_loc_fifo

# set owners and writable (since all will be r/o from cdrom)
echo setting permissions
chown -R ocaas $insdir/*
chgrp -R ocaas $insdir/*
chmod -R u+w $insdir/*

# point to shared libs
set ldc = /etc/ld.so.conf
grep -q telescope $ldc
if ($status) then
    echo adding $insdir/lib to $ldc and running ldconfig ..
    echo $insdir/lib >> $ldc
    /sbin/ldconfig
endif

# add bootup
set rcl = /etc/rc.d/rc.local
if (-e $rcl) then
    grep -q TELHOME $rcl
    if (! $status) then
	set fn = $rcl
	set tmp = $rcl.tmp
	cp $fn $tmp
	sed -e '/OCAAS/d' -e '/ocaas/d' -e '/TELHOME/d' < $tmp > $fn
	rm $tmp
    endif
    echo installing auto boot startup code in $rcl ..
    echo '' >> $rcl
    echo 'echo Starting OCAAS:' >> $rcl
    echo 'export TELHOME='$insdir >> $rcl
    echo '$TELHOME/bin/boot >> $TELHOME/archive/logs/boot.log 2>&1' >> $rcl
else
    echo no $rcl so not adding boot code.
endif

# done
echo ""
echo Installation is complete.
echo ""
echo Now you really must study the manual. Then edit the sample config files
echo in $insdir/archive/config to match your site.
echo Email support is available at ocaas@ClearSkyInstitute.com.
echo ""
echo "Welcome, OCAAS user."
echo ""
exit 0

kp:
echo ""
echo Installation has been aborted part-way through. Suggest not preserving
echo $insdir and $ohome if the installation is repeated.
echo But if you have any config files from a prior install, it might be
echo useful to copy them somewhere for reference.

/bin/kill `ps | awk '/cpio/ {print $1}'`

# For RCS Only -- Do Not Edit
# @(#) $RCSfile: install,v $ $Date: 2003/04/15 20:47:56 $ $Revision: 1.1.1.1 $ $Name:  $
