#! /bin/bash -e

# NOTE: should be invoked as either:
#    ./msys-build-openssh openssh-VER.tar.gz
#    which will unpack the "pristine" source distribution, patch it, 
#    and begin the build,
# OR, as
#    ../msys-build-openssh
#    where the current working directory is the unpacked and patched
#    source distribution.

### hardcode PKG and patches tarball
export PKG=openssh
export VER=5.4p1
export BLD=1
export SYS=msys
export SYSVER=1.0.13
FULLPKG=${PKG}-${VER}-${BLD}-${SYS} # not sysver
SRCDIR_=${PKG}-${VER}
RELDOCDIR=share/doc/${PKG}/${VER}
BINPKG=${PKG}-${VER}-${BLD}-${SYS}-${SYSVER}-bin.tar.lzma
LICPKG=${PKG}-${VER}-${BLD}-${SYS}-${SYSVER}-lic.tar.lzma
DOCPKG=${PKG}-${VER}-${BLD}-${SYS}-${SYSVER}-doc.tar.lzma
SRCPKG=${PKG}-${VER}-${BLD}-${SYS}-${SYSVER}-src.tar.lzma

BIN_CONTENTS='bin sbin etc var'
LIC_CONTENTS="${RELDOCDIR}/LICENSE"
DOC_CONTENTS="--exclude ${RELDOCDIR}/LICENSE share/doc/ share/man"

# displays error message and exits
error() {
        case $? in
                0) local errorcode=1 ;;
                *) local errorcode=$? ;;
        esac

        echo -e "\e[1;31m*** ERROR:\e[0;0m ${1:-no error message provided}";
        exit ${errorcode};
}

# displays information message
inform() {
        echo -e "\e[1;32m*** Info:\e[0;0m ${1}";
}

# displays warning message only
warning() {
        echo -e "\e[1;33m*** Warning:\e[0;0m ${1}";
}

# query
query() {
	while true
	do
          echo -e "\e[1;35m*** Query:\e[0;0m ${1}";
	  if read -e answer
	  then
	    query_result=$answer
	    return 0
	  else
	    # user did a ^D
	    echo -e "Quitting.\n"
	    exit 1
	  fi
	done
}

# displays command to stdout before execution
verbose() {
        echo "${@}"
        "${@}"
        return $?
}
export -f verbose warning inform error

cmparg=
set_cmparg() {
        case "$1" in
        *tar.bz2       ) cmparg="j" ;;
        *tar.lzma      ) cmparg=" --lzma" ;;
        *tar.xz        ) cmparg=" --use-compress-program=xz" ;;
        *tar.gz | *tgz ) cmparg="z" ;;
        *              ) cmparg="" ;;
        esac
}

specific_patchlevel=
do_patch() {
  #local patchfile_name;
  #local patchfile_path;
  #local patchlevel;
  if [ -n "$specific_patchlevel" ]
  then
    starting_patchlevel=$specific_patchlevel
    stopping_patchlevel=$specific_patchlevel
  else
    starting_patchlevel=0
    stopping_patchlevel=3
  fi
  specific_patchlevel=

  for patchfile_path
  do
    patchfile_name="${patchfile_path##*/}"

    if [ ! -e ${patchfile_path} ]
    then
      warning "patch ${patchfile_name} not found";
      continue;
    fi

    patchlevel=$starting_patchlevel

    while test $patchlevel -le $stopping_patchlevel
    do
      if patch -N -s --dry-run -p${patchlevel} -i ${patchfile_path} &> /dev/null
      then
        echo "*** applying patch ${patchfile_name}:";
        patch -N -p${patchlevel} -i ${patchfile_path} || error "patch ${patchfile_name} failed"
        break;
      elif patch -N -s --binary --dry-run -p${patchlevel} -i ${patchfile_path} &> /dev/null
      then
        echo "*** applying patch ${patchfile_name}:";
        patch -N --binary -p${patchlevel} -i ${patchfile_path} || error "patch ${patchfile_name} failed"
        break;
      elif patch -R -s --dry-run -p${patchlevel} -i ${patchfile_path} &> /dev/null
      then
        warning "patch ${patchfile_name} already applied; skipping";
        break;
      else
        if (( patchlevel == 3 ))
        then
          error patch "patch ${patchfile_name} will not apply";
        else
          patchlevel=`expr $patchlevel + 1`;
          continue;
        fi
      fi
    done
  done
}
export -f do_patch


if [ "x" != "x$1" ]; then
  if [ -d "$1" ]; then
    savedir="$PWD"
    cd "$1"
    srcdir="$PWD"
  elif [ -f "$1" ]; then
    case "$1" in
    *.tar.bz2 ) srcdir=`echo $1 | sed -e 's/\.tar\.bz2$//'`
                unpack=tbz
                savedir="$PWD" ;;
    *.tar.gz  ) srcdir=`echo $1 | sed -e 's/\.tar\.gz$//'`
                unpack=tgz
                savedir="$PWD" ;;
    *.zip     ) srcdir=`echo $1 | sed -e 's/\.zip$//'`
                unpack=zip
                savedir="$PWD" ;;
    *.tar.lzma ) srcdir=`echo $1 | sed -e 's/\.tar\.lzma$//'`
                unpack=tlz
                savedir="$PWD" ;;
    *.tar.xz )  srcdir=`echo $1 | sed -e 's/\.tar\.xz$//'`
                unpack=txz
                savedir="$PWD" ;;
    * ) error "Bad src directory specified: $1" ;;
    esac
  else
    error "Bad src directory specified: $1"
  fi
else
  savedir="$PWD"
  srcdir="$PWD"
fi

if [ "$MSYSTEM" != "MSYS" ]
then
  echo "You must be in an MSYS shell to use this script"
  exit 1
fi

if [ -n "$unpack" ] ; then
  case "$unpack" in
  tbz ) inform "unpacking $1" ; tar xjf $1 ;;
  tgz ) inform "unpacking $1" ; tar xzf $1 ;;
  zip ) inform "unpacking $1" ; unzip -q $1 ;;
  tlz ) inform "unpacking $1" ; tar --lzma -xf $1 ;;
  txz ) inform "unpacking $1" ; tar --use-compress-program=xz -xf $1 ;;
  * ) error "unknown pack format" ;;
  esac
  if [ -n "${SRCDIR_}" ]; then
    if [ ! -d "${SRCDIR_}" ]; then
      echo "src package $1 does not unpack into assumed srcdir $SRCDIR_"
      exit 1
    fi
    srcdir=`cd ${SRCDIR_} && pwd`
  fi
  if [ ! -d "$srcdir" ]; then
    echo "src package $1 does not unpack into assumed srcdir $srcdir"
    exit 1
  fi


  cd ${srcdir}
  do_patch ${savedir}/01-openssh-5.4p1-msys.patch
  cd ${savedir}
  srcdir=`cd $srcdir; pwd`
fi


abovedir=`cd ${srcdir}/..; pwd`
PREFIX=/usr
opt_flags="-O3 -fno-unit-at-a-time -s -march=i386 -mtune=i686"
export CFLAGS=${CFLAGS:-"${opt_flags}"}
export CPPFLAGS="${CPPFLAGS} -D__CYGWIN__"
export CXXFLAGS=${CXXFLAGS:-"${opt_flags}"}
export F77FLAGS=${F77FLAGS:-"${opt_flags}"}
export GCJFLAGS=${GCJFLAGS:-"${opt_flags}"}
export LDFLAGS="${LDFLAGS} -Wl,--enable-auto-import"

# NO SPACES!!
export PATH=`pwd`:/bin:/usr/local/bin:/mingw/bin:/c/WINDOWS/system32:/c/WINDOWS

mkdir -p ${abovedir}/_build
builddir=${abovedir}/_build
mkdir -p ${abovedir}/_inst
instdir=${abovedir}/_inst
cd ${builddir}

do_textreadmode() {
  if [ ! -f /usr/lib/textreadmode.o ] ; then
    cat > ${builddir}/textreadmode.c <<-"EOF"
    /* textreadmode.c

       Copyright 2004 Red Hat, Inc.

    This file is part of Cygwin.

    This software is a copyrighted work licensed under the terms of the
    Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
    details. */

    #include <windows.h>
    #include <sys/fcntl.h>
    #include <sys/cygwin.h>

    extern int _fmode;
    void
    cygwin_premain0 (int argc, char **argv, struct per_process *myself)
    {
      static struct __cygwin_perfile pf[] =
        {
          {"", O_RDONLY | O_TEXT},
          {NULL, 0}
        };
      cygwin_internal (CW_PERFILE, pf);
    }
EOF
    gcc -D__CYGWIN__ -o /usr/lib/textreadmode.o -c ${builddir}/textreadmode.c
    rm -f ${builddir}/textreadmode.c
  fi
}

msys_conf_prep() {
  inform "Preparing ${PKG}"
  cd ${srcdir}
  cp /usr/share/automake-1.11/config.guess .
  cp /usr/share/automake-1.11/config.sub .
  autoreconf -fvi
  cd ${builddir}
  do_textreadmode
  # do this so that configure will use this
  # location for ${piddir}.
  mkdir -p /var/run
}

msys_conf () {
### Configure results:
###	OpenSSH has been configured with the following options:
###	                     User binaries: /usr/bin
###	                   System binaries: /usr/sbin
###	               Configuration files: /etc/ssh
###	                   Askpass program: ${prefix}/sbin/ssh-askpass
###	                      Manual pages: /usr/share/man/manX
###	                          PID file: /var/run
###	  Privilege separation chroot path: /var/empty
###	            sshd default user PATH: /usr/bin:/bin:/usr/sbin:/sbin
###	                    Manpage format: man
###	                       PAM support: no
###	                   OSF SIA support: no
###	                 KerberosV support: no
###	                   SELinux support: no
###	                 Smartcard support:
###	                     S/KEY support: no
###	              TCP Wrappers support: no
###	              MD5 password support: no
###	                   libedit support: no
###	  Solaris process contract support: no
###	       IP address in $DISPLAY hack: no
###	           Translate v4 in v6 hack: no
###	                  BSD Auth support: no
###	              Random number source: OpenSSL internal ONLY
###	
###	              Host: i686-pc-msys
###	          Compiler: gcc
###	    Compiler flags: -O3 -fno-unit-at-a-time -s -march=i386 -mtune=i686 
###			    -Wall -Wpointer-arith -Wuninitialized 
###			    -Wsign-compare -Wformat-security
###			    -fno-builtin-memset
###	Preprocessor flags:  -D__CYGWIN__
###	      Linker flags:  -Wl,--enable-auto-import
###	         Libraries: -lcrypto -lz  -lminires /usr/lib/textreadmode.o -lcrypt
###	
###	WARNING: the operating system that you are using does not
###	appear to support getpeereid(), getpeerucred() or the
###	SO_PEERCRED getsockopt() option. These facilities are used to
###	enforce security checks to prevent unauthorised connections to
###	ssh-agent. Their absence increases the risk that a malicious
###	user can connect to your agent.

  confargs="--prefix=${PREFIX} --libexecdir=\${sbindir} \
	    --datarootdir=\${prefix}/share --docdir=\${prefix}/${RELDOCDIR} \
	    --without-rpath --sysconfdir=/etc/ssh --with-mantype=man \
	    --with-libs=-lminires"

  ${srcdir}/configure --srcdir=${srcdir} ${confargs} \
	CFLAGS="${CFLAGS}" \
	CPPFLAGS="${CPPFLAGS}" \
	CXXFLAGS="${CXXFLAGS}" \
	LDFLAGS="${LDFLAGS}"
}

msys_build () {
  inform "Building ${PKG}"
  make
}

msys_check () {
  :
}

msys_install () {
  inform "Installing ${PKG}"
  make install DESTDIR=${instdir}

  verbose rm -f ${instdir}${PREFIX}/share/info/dir
  verbose rm -f ${instdir}${PREFIX}/sbin/sshd.exe
  verbose rm -f ${instdir}${PREFIX}/sbin/sftp-server.exe
  verbose rm -f ${instdir}/etc/ssh/sshd_config
  verbose rm -f ${instdir}${PREFIX}/share/man/man8/sftp-server.8
  verbose rm -f ${instdir}${PREFIX}/share/man/man8/sshd.8
  verbose rm -f ${instdir}${PREFIX}/share/man/man5/sshd_config.5

  verbose mkdir -p ${instdir}${PREFIX}/${RELDOCDIR}
  verbose cp -p ${srcdir}/ChangeLog*\
	${srcdir}/CREDITS \
	${srcdir}/LICENCE \
	${srcdir}/OVERVIEW \
	${srcdir}/README \
	${srcdir}/README.dns \
	${srcdir}/README.platform \
	${srcdir}/README.privsep \
	${srcdir}/README.tun \
	${srcdir}/TODO \
	${srcdir}/WARNING.RNG \
	${srcdir}/contrib/cygwin/ssh-user-config ${instdir}${PREFIX}/${RELDOCDIR}

  #correct spelling error
  verbose mv ${instdir}${PREFIX}/${RELDOCDIR}/LICENCE \
  	${instdir}${PREFIX}/${RELDOCDIR}/LICENSE

  verbose /usr/bin/install -d -m 755 ${instdir}${PREFIX}/share/doc/MSYS
  verbose /usr/bin/install -m 644 ${abovedir}/msys-openssh.RELEASE_NOTES \
          ${instdir}${PREFIX}/share/doc/MSYS/${PKG}-${VER}-${BLD}-${SYS}.RELEASE_NOTES.txt
  verbose /usr/bin/install -d -m 755 ${instdir}/var/run

  # We 'tar' from ${instdir}/usr, so as to move everything from
  # /usr/* to /* in the tarball to support the /usr == / mount.
  # However, this means that ${instdir}/var and ${instdir}/etc
  # will be missed. So:
  if [ -d ${instdir}/var ]
  then
    verbose mv ${instdir}/var ${instdir}${PREFIX}/
  fi
  if [ -d ${instdir}/etc ]
  then
    verbose mv ${instdir}/etc ${instdir}${PREFIX}/
  fi
}

msys_package () {
  inform "Packaging ${PKG}"
  cd ${instdir}${PREFIX}

  set_cmparg "${BINPKG}"
  tar cv${cmparg} --hard-dereference -f ${abovedir}/${BINPKG} ${BIN_CONTENTS}

  set_cmparg "${LICPKG}"
  tar cv${cmparg} --hard-dereference -f ${abovedir}/${LICPKG} ${LIC_CONTENTS}

  set_cmparg "${DOCPKG}"
  tar cv${cmparg} --hard-dereference -f ${abovedir}/${DOCPKG} ${DOC_CONTENTS}

  cd ${abovedir}
  set_cmparg "${SRCPKG}"
  tar cv${cmparg} -f ./${SRCPKG} \
    ${PKG}-${VER}.tar.gz \
    01-openssh-5.4p1-msys.patch \
    msys-openssh.RELEASE_NOTES \
    msys-build-openssh
}

msys_conf_prep
msys_conf
msys_build | tee ${PKG}-build.log 2>&1
msys_check
msys_install
msys_package

cd "$savedir"

