#!/bin/sh
set -e

# Inspired by the metrox post-install script written by
# Johnie Ingram <johnie@debian.org>
# Modified by Gopal Narayanan <gopal@debian.org>


#DEBHELPER#


if [ "$0" = "abort-upgrade" ]
then
    exit 0
fi


VERSION=3.03

#if [ -f /usr/bin/setiathome ]
#then
#   RVERSION=`/usr/bin/setiathome -version|grep Version|cut -d' ' -f2-`
#echo "Current Version $RVERSION"
#fi


#if [ "${RVERSION}" != "${VERSION}" ]
#then

DOWNLOAD=ftp://ftp.cdrom.com/pub/setiathome
#DOWNLOAD=ftp://alien.ssl.berkeley.edu/pub
PROGNAME=setiathome
PREFIX=/usr
#MACH=`uname -m`
MACH=`dpkg --print-installation-architecture`

case "$MACH" in 
    *86)
    MACH=`uname -m`
	if [ $MACH != "i686" ]
	then
	    MACH=i386
	    MD5="6c0e5cd95c8005ffb658f968120be453"
	else
	    MD5="ba17d596fd07219c9c0eac4c1d5597a7"
	fi
	SUFFIX=-pc-linux-gnu-gnulibc2.1
	DIRECTORY=$PROGNAME-$VERSION.$MACH$SUFFIX
	INNARDS=$DIRECTORY.tar
     ;;

     sparc)
#        VERSION=2.0
	SUFFIX=-unknown-linux-gnu
	DIRECTORY=$PROGNAME-$VERSION.$MACH$SUFFIX
	INNARDS=$DIRECTORY.tar
	MD5="1091488f55cbbfa4451c3f03cc9f8177"
     ;;

     alpha)
#	VERSION=2.4
	SUFFIX=-unknown-linux-gnu
#	MACH=alphaev5
	DIRECTORY=$PROGNAME-$VERSION.$MACH$SUFFIX
	INNARDS=$DIRECTORY.tar
	MD5="ddc9d38ffb5bc7c1189857fc054b7252"
     ;;

    powerpc)
#        VERSION=2.0
        SUFFIX=-unknown-linux-gnu
        DIRECTORY=$PROGNAME-$VERSION.$MACH$SUFFIX
        INNARDS=$DIRECTORY.tar
#	MD5="d7f3860cf5ffd72c5c48588307891cb4"
#	MD5="805beae8e2d553b933af206db4bd6e35"
	MD5="cb47ccfd01e6f9764edb74ce37aa216b"
      ;;
      
     hppa)
	SUFFIX=parisc-palinux
	DIRECTORY=$PROGNAME-$VERSION.$MACH-$SUFFIX
	INNARDS=$DIRECTORY.tar
	MD5=8aaeb4f16b4703d6f9cbf91a75ebb0b4
      ;;

     ia64)
	SUFFIX=Linux-gnu
	DIRECTORY=$PROGNAME-$VERSION.$MACH-$SUFFIX
	INNARDS=$DIRECTORY.tar
	MD5=b52d2b0c29b07a30c5abdb6415927506
      ;;

     *)
        echo "No setiathome binary available for architecture $MACH"
	exit 1
     ;;
esac


if [ "$TMPDIR" != "" ]
then
    TDIR=$TMPDIR
else
    TDIR=/tmp
fi


#echo $MACH


#XTDIR=$TDIR/setiathome-inst
XTDIR=`tempfile`
DELFILES="$XTDIR"
rm -f ${XTDIR}
cd $TDIR

if [ -d ${XTDIR} ]
then 
    echo Directory $XTDIR already exists.
    echo Please remove $XTDIR and try again.
    exit 1
fi 
    

if [ -f ${INNARDS} ]
then
#    if [ -f ${INNARDS}.gz -o -f ${INNARDS}.Z ]
#    then
#	zopt="-z"
#    else
#	zopt=""
#    fi
    MD5C=`md5sum ${INNARDS} | cut -f1 -d' '`
    if test $MD5C = $MD5; then
	echo -n "Untarring ${INNARDS} ... "
	mkdir -p $XTDIR
	cd $XTDIR
	if tar -xf ../${INNARDS}* 2> /dev/null
	then
	    echo done.
	else
	    echo failed!
	    echo -n "Cleaning up ... "
	    cd $TDIR && rm -rf $DELFILES
	    echo "done (sigh)."
	    exit 1
	fi
     else
        echo tar file insecure. md5sum failed!
	echo Make sure ${INNARDS} is the right tar file
	exit 1
     fi
else
    echo ${INNARDS} not found in $TDIR.
    if [ -f /usr/bin/wget ] 
    then 
#	echo -n "Shall I retrieve ${INNARDS} for you from the web site [Y/n] ?"
#	read ans
#	case "$ans" in
#	    [Nn]*)  exit 1 ;;
#	esac
#	/usr/bin/wget -nd --passive-ftp -P $TDIR $DOWNLOAD/$INNARDS
	temp=`tempfile`
	/usr/bin/wget -nd --passive-ftp -O $temp $DOWNLOAD/$INNARDS
	MD5C=`md5sum ${temp} | cut -f1 -d' '`
	if test $MD5C = $MD5; then	
	    echo -n "Untarring ${INNARDS} ... "
	    mkdir -p $XTDIR
	    cd $XTDIR
	    if tar -xf ${temp} 2> /dev/null
	    then
		echo done.
	    else
		echo failed!
		echo -n "Cleaning up ... "
		cd $TDIR && rm -rf $DELFILES
		echo "done (sigh)."
		exit 1
	    fi
	 else
	    echo tar file insecure. md5sum failed
	    echo Make sure ${temp} is the right tar file
	    exit 1
	 fi	 
    else 
        echo "wget not available for automatic retrieval of tar file"
	echo "Please get ftp://alien.ssl.berkeley.edu/pub/$INNARDS and put it into $TDIR"
	echo "And then try reinstalling..."
	echo
	exit 1
    fi
fi

echo -n "Installing SETI@Home client v$VERSION: "
#chown -R root.root $XTDIR
#chmod -R u=rwX,go=rX $XTDIR
chmod +x $XTDIR/$DIRECTORY/$PROGNAME

echo -n "copying.."
echo -n "."
#cp -dpRf $XTDIR/$DIRECTORY/setiathome $PREFIX/bin/setiathome
install -g root -o root -m 755 $XTDIR/$DIRECTORY/setiathome $PREFIX/bin/setiathome
echo -n "."

echo ". done."

echo -n "installing manpages.."

case "$MACH" in 
    *)
    cat <<EOF > /usr/share/man/man1/setiathome.1
.TH SETIATHOME 1 "October 11, 2000"
.\" Author: Gopal Narayanan <gopal@debian.org>
.\" Based on the README file that accompanies the seti@home
.\" client tar file
.SH NAME
setiathome \- the SETI@home client program
.SH SYNOPSIS
.B setiathome 
[\c
.BI options\fR\c
]

.SH DESCRIPTION
.LP
.B setiathome 
is the UNIX version of the SETI@home client. It
downloads radio telescope data from a network server, analyzes 
the data looking for signals of extraterrestrial origin, and 
uploads results to the server, repeating this cycle indefinitely.
See http://setiathome.ssl.berkeley.edu for more information.
.SH FILES
.LP
The program generates several files with .sah extension in the
directory from which it is run.  These should not be modified.
.LP
If you want to run multiple instances of setiathome (on a
multiprocessor machine, or on multiple machines that share a
filesystem) each one must be run in a different directory. You can use 
symlinks for each directory to avoid duplication of the binary image.
e.g.:
.LP
.nf
ln -s cpu0/setiathome cpu1/setiathome
.fi
.LP
.B setiathome 
uses a lock file (lock.txt) to prevent multiple instances from running
in the same directory.
.LP
To finish up your current work unit, return your result, and not
download a new work unit, while the client is running, touch
a file named "stop_after_send.txt" in the client directory.
When the processing is finished, and the result sent, the client
will stop. 
.B *** 
Note this one file extension is .txt, while all other 
files used by the client have extensions .sah 
.B ***
.LP
The file "pid.sah" contains the process ID of the current instance.
.SH RUNNING SETIATHOME
.LP
It is suggested that
the user create a new directory, say, 
.I \&$HOME/setiathome
(henceforth referred to as SETIDIR)
and then cd into SETIDIR, before running 
.B setiathome 
for the first time. The first time you run 
.B setiathome 
it will interactively
ask you for email address, name, country etc. 
This info is stored 
in a file and no interaction is needed when you run the program 
subsequently.
.LP
After this you can run 
.B setiathome 
in the background, and direct its output to /dev/null if you like.
.LP
.B setiathome 
can be freely aborted and restarted. It saves its state in files, 
and will pick up where it left off.
.LP
If you want
.B setiathome 
to be started automatically, you may wish to setup a cron job. 
If you add the following line to your crontab:
.LP
.nf
0 * * * * cd SETIDIR; setiathome -nice 19 > /dev/null 2> /dev/null
.fi
.LP
(where SETIDIR is the directory chosen above  - where you normally run
the client program from),
this cron job will attempt to start the client
at the top of every hour.  If it is already running, the next
invocation will do nothing.  If the client is not running, it will
be started.  For more information on cron jobs see the 
.BR crontab(1)
manual page.
.LP
The following script will stop all instances 
.B SETI@home:
(assuming the binary execution name is: setiathome)
.LP
.nf
#! /bin/sh
kill \`ps aux | grep setia | grep -v grep | awk '{print $2}'\`
.fi
.LP
The following will stop the instance in the current directory:
.LP
.nf
#! /bin/sh
kill \`cat pid.sah\`
.fi
.LP
Please do not operate the client on machines for which you do not
have permission.
.LP
.SH ENVIRONMENT VARIABLES
.LP
If the environment variable HTTP_PROXY is defined, setiathome will
connect through a proxy server, specified as hostname or hostname:port.
.LP
If the environment variable SOCKS_SERVER is defined, setiathome will
connect through a SOCKS server, specified as hostname or
hostname:port.  If the environment variables SOCKS_USER and
SOCKS_PASSWD are defined, these will be used as the login name and
password for the SOCKS server.  Otherwise setiathome will query you.
.LP
.ne 5
.SH OPTIONS
.TP
.B \-login
Login or create new account.
.ne 3
.TP
.B \-countries
Show list of country codes.
.ne 3
.TP    
.B \-version
show software version
.ne 3
.TP        
.B \-nice  \ N
set "nice" priority to N (default 1);
.ne 3
.TP    
.B \-email
Send email (to login email address) on errors.
Useful if you run in background directed to /dev/null.
.ne 3
.TP
.B \-graphics
Generate a data stream for the xsetiathome graphical interface
(available from version 2). This option is not available for all clients.
The debian version of the setiathome package currently does not install 
xsetiathome currently, due to a X-library incompatibility of the precompiled 
binary.
.ne 3
.TP    
.B \-proxy \ hostname:port
Connect to SETI@home server via specified proxy server and port.
.ne 3
.TP
.B \-socks_server \ hostname:port
Connect to SETI@home server via specified SOCKS server and port.
Overrides SOCKS_SERVER. SOCKS versions 4 and 5 are supported.
.ne 3
.TP
.B \-socks_user \ name
SOCKS user name.
.ne 3
.TP
.B \-socks_password \ password
SOCKS password.
.ne 3
.TP    
.B \-stop_after_process
If a work_unit.sah file is present, process this work unit
and stop after processing is complete, do not return result.
If the client is started with this option, and there is
a completed result.sah file present, the client will first
return the results, then pick up a new work unit, process
to completion, then exit.
.ne 3
.TP    
.B \-stop_after_xfer
Return results and pick up a new work unit.  This option only
functions if the result.sah file is present and complete indicating
that processing is finished for this
work unit.  If a work_unit.sah is present, indicating processing
is not complete, no transfer or processing will be performed,
and the client will exit.
.ne 3
.TP
.B \-verbose
print a running summary of the work being done.
Starting with the version 2.4 clients, the client is silent
to stdout during processing.  There are messages at the start 
and finish of processing a work unit, but there are no progress
messages printed during processing unless this option is used.
.ne 3
.TP
.B \-nolock
omit the multiple-instance check, which uses file locking
(not available on some NFS systems)
.SH KNOWN BUGS:
.LP

Outstanding shared memory segments and semaphores may be left active
in case of an abnormal exit of the 'setiathome -graphics'
process.  These can prevent any future invocation of
'setiathome -graphics'.  To resolve this problem, use 'ipcs'
and 'ipcrm' to remove shared memory segments and semaphores that
are not associated with a process.  This behavior may vary
depending upon how your UNIX system handles this situation.
See also 
.BR ipcs(1) 
and 
.BR ipcrm(1)
.LP
For version 3.0 clients, the estimated progress as indicated
by the prog= line in the state.sah file in not exactly linear
in relationship to completion time.  Using this value to
predict completion time may not be completely accurate.
The linear relationship will vary depending upon the characteristics
of the work unit parameters.
.LP
.SH SEE ALSO:
There is much more information to be found about the operation
of the client at the following WEB sites:
.nf
http://setiathome.berkeley.edu/
http://setiathome.berkeley.edu/faq.html
http://setiathome.berkeley.edu/links.html
.fi
with discussions of add-on programs and scripts to control
the client in various situations.
.LP
.SH AUTHOR
.LP
This manual page was written by Gopal Narayanan
<gopal@debian.org>,
for the Debian GNU/Linux system (but may be used by others). This
manual page is mostly derived from the README file that accompanies the 
.B setiathome
client program.
EOF
	    /bin/gzip -9 /usr/share/man/man1/setiathome.1
    ;;

esac



echo -n Cleaning up...
cd $TDIR && rm -rf $DELFILES
echo -n Removing temp files...
rm -f ${temp}
echo "done."
#echo Remember to delete ${INNARDS} in $TDIR.
echo " "

#else 
# echo "Debian release change. You already have the right upstream binary"
#fi

#set doc links

if [ "$1" = "configure" ]; then
        if [ -d /usr/doc -a ! -e /usr/doc/setiathome -a -d /usr/share/doc/setiathome ]; 
then
                ln -sf ../share/doc/setiathome /usr/doc/setiathome
        fi
fi


#ldconfig

