#!/bin/sh
## Configuration wrapper for openssh, CERN environment
# Master source is from the SRPM. Do the equivalent of a rpm -i; rpm -bp:
# unpack SRPM, unpack tar.gz, apply all patches
#

SYS=`/usr/afsws/bin/sys`
THIS_DIR=/afs/cern.ch/project/connectivity/openssh-3.1p1
ASKPASS_DIR=/afs/cern.ch/project/connectivity/x11-ssh-askpass-1.1.1/$SYS
SSL_DIR=/afs/cern.ch/project/connectivity/openssl-0.9.6
KRB_DIR=/afs/cern.ch/project/connectivity/krb4-1.1

config_options="--sysconfdir=/etc \
		--bindir=/usr/sue/bin \
		--sbindir=/usr/sue/etc \
		--libexecdir=/usr/sue/etc \
                --localstatedir=/var \
                --with-ssl-dir=$SSL_DIR/$SYS/install \
                --with-kerberos4=$KRB_DIR/$SYS \
                --with-afs \
		--disable-suid-ssh \
		--with-zlib=/afs/cern.ch/project/connectivity/zlib-1.1.4/$SYS \
		--with-default-path=/usr/bin:/bin:/usr/sbin:/sbin:/usr/sue/bin \
                --with-ip4-default=yes \
                "
# additional hacks after running "configure"
echo "addtional hacks: remove all /usr/local things from Makefile, remove -lpam from everything but the sshd, remove libdes"
post_config_hack="for makefile in \`find . -name Makefile -print\`;
      do echo cleaning up \$makefile;
             ( echo '1,\$s;\(-[RLI]\)/usr/local;\1/usr_local_nowhere;g' ;
               echo '1,\$s;-lpam;;g' ;
               echo '1,\$s;\$(SSHDOBJS) \$(LDFLAGS) -lssh;& -lpam;g' ;
               echo '1,\$s;-ldes;;g' ;
               echo 'wq' ) | ed \$makefile; done; echo hacks done;"

# override the early "-I/usr/local/include" by configure
CFLAGS="-g -O -I$SSL_DIR/$SYS/install/include \
           -I$KRB_DIR/$SYS/include "
export CFLAGS

# override /usr/local/lib as well
LDFLAGS="-z muldefs -L$SSL_DIR/$SYS/install/lib \
         -L$KRB_DIR/$SYS/lib"
export LDFLAGS

# prefer relative paths for ssh, ssh-askpass
# NO. scripts tend to break
#MAKEFLAGS="SSH_PROGRAM=ssh ASKPASS_PROGRAM=ssh-askpass"
#export MAKEFLAGS
MAKE_LOG=make.log

machtype=`eval uname`
machvers=`eval uname -r`
echo "Configuring for $machtype"

# how to get dynamic linked libraries for an executable
ldd="/unconfigured_ldd"
grep=/usr/local/bin/ggrep
# regexp to get shared libraries from $ldd
ldd_libs=".*"
# regexp whitelist of "good" libraries
allowed_libs=""

case "$machtype" in
	Linux*)
	 config_options="$config_options --with-tcp-wrappers --with-pam=yes "
         ldd="ldd"
	 ldd_libs="lib"
	 allowed_libs="libpam\.\\|libz\.\\|libresolv\.\|libnsl\.\|libutil\.\|libc\.\|ld-linux\."
	 grep=grep
	 ;;
	AIX*)
	 config_options="$config_options --with-pid-dir=/etc"
	 ldd="dump -H"
	 ldd_libs="^[1-9][0-9]*.*lib"
	 allowed_libs="libc\.\|libnsl\."
	 ;;
        IRIX*)                # no /var/run, so use /etc for pid-file
	 config_options="$config_options --with-pid-dir=/etc"
         ldd="odump -Dl"
	 ldd_libs="lib"
	 allowed_libs="libc\."
	 ;;
	SunOS*)
	 config_options="$config_options --with-pid-dir=/etc  --with-pam=yes  --with-tcp-wrappers=/afs/cern.ch/project/connectivity/tcp_wrappers/$SYS"
	 CC=cc; export CC
         ldd="ldd"
	 ldd_libs="lib"
	 allowed_libs="libresolv\.\|libsocket\.\|libnsl\.\|libc\.\|libdl\.\|libmp\.\|libc_psr\.\|libpam\."
	 ;;
	HP-UX*)
	# /usr/include/prot.h is more important than 
        #     .../athena/include/prot.h due to the definition of pr_passwd
        # BUT varargs.h needs to come from the gcc include dir (and not from
        #  /usr/include), otherwise we'll have va_start wrong. YUK.
        #
	# _REENTRANT is needed according to the mailing list
                case "$machvers" in
                    [AB].09.*)
	 CFLAGS="-I/usr/include -DRLIMIT_CORE=4 $CFLAGS"
                     ;;
                    *)
	 CFLAGS="-D_REENTRANT \
                 -I/usr/local/lib/gcc-lib/hppa1.1-hp-hpux10.20/egcs-2.91.66/include \
                 -I/usr/include $CFLAGS"
                     ;;
                esac
	 export CFLAGS
         ldd="chatr"
	 ldd_libs="dynamic.*lib"
	 allowed_libs="libsec\.\|libc\."
	 ;;
	OSF*)
	# SEGV in krb_afslog() possibly due to some SIA overwriting? 
        # Make sure we don't use SIA since it is still experimental.
         USE_SIA=""; export USE_SIA
         ldd="odump -Dl"
         ldd_libs="lib"
	 allowed_libs="libc\."
esac

###### CONFIGURATION, BUILDING, INSTALLING #################
exec > $MAKE_LOG 2>&1
set -x
echo "Configuring with ./configure $config_options"
echo "##### Building environment ######" 
env 
echo "##### End of Building environment ######" 
( ./configure $config_options && \
  eval $post_config_hack && \
  gmake $MAKEFLAGS && \
  echo "##### Installation ######" && \
  rm -rf install && \
  gmake -k DESTDIR=./install \
           bindir= sbindir= mandir= libexecdir= sysconfdir= \
           INSTALL_SSH_PRNG_CMDS=yes install && \
  echo gmake clean && \
  echo "##### x11-ssh-askpass Installation ######"  && \
  cp $ASKPASS_DIR/x11-ssh-askpass ./install/x11-ssh-askpass && \
  cp $ASKPASS_DIR/x11-ssh-askpass.man \
        ./install/man1/x11-ssh-askpass.1 && \
  cd install && \
  ln -sf x11-ssh-askpass ssh-askpass && \
  cd man1 && \
  ln -sf x11-ssh-askpass.1 ssh-askpass.1 ) 
              

echo "don't forget libafs.so from krb4-1.0.4 if you are on AIX!!"

###### TEST FOR BAD DLLs  ##########
echo "##### dll tests #####" >> $MAKE_LOG
( for i in ssh sshd ssh-keygen scp sftp-server ssh-add; do \
  if ${ldd} install/$i | $grep ${ldd_libs} | $grep -v ${allowed_libs}; then \
    echo "binary install/$i is dynamically linked to wrong library."; \
    exit 3; \
  fi ;\
done ) 

##### save the log file
cp $MAKE_LOG install/