#!/bin/sh
[ `id -u` = 0 ] || { echo "must be root"; exit 1; }

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-Wl,-O1"

MIRROR="ftp://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/"
PACKAGE="openssh"
SEP="-"
VERSION="5.8p1"
DESCRIPTION="OpenSSH Server and Client"
AUTHORS="Many contributors, see site"
HOMEPAGE="http://www.openssh.org"
LICENSE="BSD"
ME="Kingdomcome"
DEPS="openssl-0.9.8.tcz gcc_libs.tcz"
BUILDDEPS="openssl-0.9.8-dev.tcz"
FLAGS="--prefix=/usr/local
       --sysconfdir=/usr/local/etc/ssh
       --with-privsep-user=nobody
       --with-privsep-path=/usr/local/var/ssh
       --with-xauth=/usr/bin/xauth
       --with-default-path=${PATH}"
DOCS="usr/local/share/man"
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"

environment(){
   for each in compiletc.tcz squashfs-tools-4.x.tcz ${DEPS} ${BUILDDEPS}; do
      sudo -u ${TCUSER} tce-load -w -i ${each}
   done
   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -p ${PKGDIR}/tmp
   cd ${PKGDIR} && wget ${MIRROR}${SRC}
   tar xzf ${SRC}
}

buildit(){
   cd ${SRCDIR}
   ./configure ${FLAGS}
   make
   make DESTDIR=${PKGDIR}/tmp install
}

workit(){
   cd ${PKGDIR}/tmp
   rm -rf ${DOCS}

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/LICENCE usr/local/share/doc/License/${PACKAGE}.txt

   find usr/ | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
   find usr/ | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null

#   mkdir -pm 775 usr/local/tce.installed
#cat <<EOF> usr/local/tce.installed/${PACKAGE}
##!/bin/sh
#EOF
#   chmod 775 usr/local/tce.installed/${PACKAGE}

#   chown -R root:staff usr/local/tce.*

# package specific
   mv usr/local/etc/ssh/ssh_config usr/local/etc/ssh/ssh_config.example
   mv usr/local/etc/ssh/sshd_config usr/local/etc/ssh/sshd_config.example
   mkdir -pm 755 usr/local/etc/init.d/
cat <<EOF>usr/local/etc/init.d/openssh
#!/bin/sh
# openssh sshd start script
[ \$(id -u) = 0 ] || { echo "must be root" ; exit 1; }

start(){
   [ -f /usr/local/etc/ssh/sshd_config ] || { echo "Config file /usr/local/etc/ssh/sshdd_config not found"; exit 1; }
   [ -f /usr/local/etc/ssh/ssh_host_rsa_key ] || ssh-keygen -t rsa -N "" -f /usr/local/etc/ssh/ssh_host_rsa_key
   [ -f /usr/local/etc/ssh/ssh_host_dsa_key ] || ssh-keygen -t dsa -N "" -f /usr/local/etc/ssh/ssh_host_dsa_key
   [ -f /usr/local/etc/ssh/ssh_host_ecdsa_key ] || ssh-keygen -t ecdsa -N "" -f /usr/local/etc/ssh/ssh_host_ecdsa_key
   /usr/local/sbin/sshd
}

stop(){
   kill \$(pidof sshd)
}

restart(){
   if pidof sshd >/dev/null; then
      stop && start
   else
      start
   fi
}

keygen(){
   ssh-keygen -t rsa -f /usr/local/etc/ssh/ssh_host_rsa_key
   ssh-keygen -t dsa -f /usr/local/etc/ssh/ssh_host_dsa_key
}

case \$1 in
   start) start;;
   stop) stop;;
   restart) restart;;
   keygen) keygen;;
   *) echo "Usage \$0 {start|stop|restart|keygen}"; exit 1
esac
EOF
   chmod 755 usr/local/etc/init.d/openssh
}

packageit(){
   cd ${PKGDIR}/tmp
   for dir in `ls`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}.tcz.list
   done
   mksquashfs . ../${PACKAGE}.tcz
   cd ${PKGDIR}
   md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt
   [ -f "${PACKAGE}.tcz.dep" ] && rm -f ${PACKAGE}.tcz.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.tcz.dep; done
size=`du -h ${PACKAGE}.tcz | cut -f 1`
today=`date +%Y/%m/%d`
cat <<EOF> ${PACKAGE}.tcz.info
Title:		${PACKAGE}.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	This extension depends on openssl-0.9.8.tcz and
		gcc_libs.tcz
		Config files are located at /usr/local/etc/ssh/
		Copy ssh_config.example and sshd_config.example to
		ssh_config and sshd_config AND EDIT to fit setup.
		
		**Warning: Default sshd_config contains possible
		security concerns.
		
		Host key files will need to be generated only
		if you intend to run a server.
		Use /usr/local/etc/init.d/openssh to start, stop or
		restart server and to generate host keys.
		
		This extension is PPI compatible
Change-log:	2009/05/15 First Version
		2009/09/02 Rebuilt to not use user.tar.gz
		           Removed host key generation from post-install
		2009/09/15 Moved license to proper directory
		2009/10/12 Bumped to 5.3p1
			   Removed libssp and added gcc_libs.tczl as dep
		2009/11/25 Added script to generate host keys
		2009/11/29 Removed keygen script in favor of
			   all-inclusive init script
		2010/02/27 Rebuilt against openssl-0.9.8m
		2010/03/14 Removed copying of example configs to default
			   config name
			   Reworded info
		2010/03/25 Fixed bug in init script
		2010/04/04 Bumped to 5.4p1
			   Rebuilt against openssl-0.9.8(n)
Current:	2010/06/21 Bumped to 5.5p1
			   Rebuilt against openssl-0.9.8(o)
Current:	${today} Bumped to 5.8p1
			   Rebuilt against openssl-0.9.8(p)
EOF
}
# 2009/05/15 First Version
# 2009/09/02 rebuit removing user.tar.gz, removed host keygen from
#            post-install
# 2009/09/15 moved License to proper dir
# 2009/10/12 Bumped to 5.3p1, removed libssp and added gcc_libs dep
# 2009/11/25 Added host key script
# 2009/11/29 Removed keygen script in favor of all inclusive init.d script
# 2010/02/27 Rebuilt against openssl-0.9.8m
# 2010/03/14 dont copy config, update info
# 2010/03/25 fixed init script bug
# 2010/04/04 bumped to 5.4p1, rebuilt against openssl-0.9.8(n)
# 2010/06/21 bumped to 5.5p1, rebuilt against openssl-0.9.8(o)
# 2011/02/17 bumped to 5.8p1, rebuilt against openssl-0.9.8(p)

#here we go

environment
buildit
workit
packageit
