#!/bin/sh

INSTALL_DIR=`pwd`
SOFTWARE=openssh
VERSION=4.2p1
SFTPLOGGING_VERSION=1.4

function cleanupandexit {
  echo "Installation failed.  Look above for errors."
  cd $INSTALL_DIR
  rm -rf ${SOFTWARE}-${VERSION}
  exit 1
}

tar xzf ${SOFTWARE}-${VERSION}.tar.gz || cleanupandexit

echo "*** Applying osshChroot-${VERSION}.diff ***"
patch -p0 < $INSTALL_DIR/patches/osshChroot-${VERSION}.diff || cleanupandexit
echo "*** Applying openssh-${VERSION}.sftplogging-v${SFTPLOGGING_VERSION}.patch ***"
patch -p0 < $INSTALL_DIR/patches/openssh-${VERSION}.sftplogging-v${SFTPLOGGING_VERSION}.patch || cleanupandexit

cd $INSTALL_DIR/${SOFTWARE}-${VERSION} || cleanupandexit

./configure --prefix=/opt/${SOFTWARE}/current --with-pam --with-privsep-user=sshd --without-zlib-version-check --with-tcp-wrappers --with-default-path=/usr/local/bin:/bin:/usr/bin --with-superuser-path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin --with-privsep-path=/var/empty/sshd --with-kerberos5=`krb5-config --prefix` --disable-strip || cleanupandexit

mkdir -p /opt/${SOFTWARE}

make || cleanupandexit
make install || cleanupandexit

cd /opt/${SOFTWARE}
mv current ${SOFTWARE}-${VERSION}-sftplogging-v${SFTPLOGGING_VERSION}-chroot
ln -s ${SOFTWARE}-${VERSION}-sftplogging-v${SFTPLOGGING_VERSION}-chroot current

echo ""
echo "OpenSSH (with ChRoot & SFTPLogging Patches) Installation Complete"

