#!/bin/sh
#
#  This file may help you build the dependency packages that
#  are needed to cross compile the Win64 bit version of the Bacula
#  File daemon.  This file is provided as is, and we don't guarantee
#  that it will work. We run it only on Ubuntu Hardy.  Trying to use
#  it on any other GNU/Linux distro will probably require changes.
#
#  This file is driven by the parameters that are defined in
#    the file External-mingw-w64
#

usage()
{
   echo "usage: $0 [-h] [-C] [<dependency 1>] [<dependency 2>] ..."
   echo "       -h      Displays this usage"
   echo "       -C      Clobbers (overwrites) the source code by "
   echo "               reextracting the archive and reapplying the"
   echo "               patches."
   echo ""
   echo "<dependency N> Optional dependency, If none are given then all"
   echo "               of them will be built."
   echo ""
   echo "Valid dependencies are:"
   grep -v '^#' < External-mingw-w64 | cut -d'|' -f1 | cut -d'_' -f1 | tr A-Z a-z | sort -u | awk '{ print "        " $1 }'
}

CLOBBER_SOURCE=

while getopts "hHC" opt; do
   case ${opt} in
   H|h|\?) usage;exit 1;;
   C)      CLOBBER_SOURCE=true;;
   esac
done

[ ${OPTIND} -gt 1 ] && shift `expr ${OPTIND} - 1`

cwd=`pwd`
cd `dirname $0`
SCRIPT_DIR=`pwd`

cd ../../..
TOP_DIR=`pwd`
TOP_DIR=${DEPKGS:-${TOP_DIR}}

if [ -e ${TOP_DIR}/cross-tools/mingw-w64/bin/x86_64-pc-mingw32-gcc ]
then
   cd ${TOP_DIR}/cross-tools/mingw-w64/bin
   BIN_DIR=`pwd`
else
   echo "The GCC cross compiler is not installed."
   echo "You must run build-win64-cross-tools first"
   exit 1
fi

[ ! -e ${TOP_DIR}/depkgs-mingw-w64 ] && mkdir ${TOP_DIR}/depkgs-mingw-w64
cd ${TOP_DIR}/depkgs-mingw-w64
DEPPKG_DIR=`pwd`

export PATH=${BIN_DIR}:${PATH}

[ ! -e bin ] && mkdir bin
[ ! -e src ] && mkdir src
[ ! -e include ] && mkdir include
[ ! -e lib ] && mkdir lib

OLD_IFS=${IFS};IFS="|";
while read package url dir mkd; do
   echo "Got package ${package}"
   case ${package} in
   \#*) ;;
   *) eval "URL_${package}=${url};DIR_${package}=${dir};MKD_${package}=${mkd}";;
        esac
done < ${SCRIPT_DIR}/External-mingw-w64
IFS=${OLD_IFS};unset OLD_IFS

get_source()
{
   URL=$1
   SRC_DIR=$2
   MAKE_DIR=$3
   echo "Processing ${URL}"
   ARCHIVE=`basename ${URL}`
   
   case ${ARCHIVE} in
   *.tar.gz)       ARCHIVER="tar xzf";    [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.tar\.gz'`;;
   *.tar.bz2)      ARCHIVER="tar xjf";    [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.tar\.bz2'`;;
   *.zip)          ARCHIVER="unzip -q";   [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.zip'`;;
   *.exe)          ARCHIVER="";           [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.zip'`;;
   *)              echo Unsupported archive type - $ARCHIVE; exit 1;;
   esac
   
   cd ${DEPPKG_DIR}/src
   
   if [ ! -e "${ARCHIVE}" ]
   then 
      echo Downloading "${URL}"
      if wget --passive-ftp "${URL}"
      then
         :
      else
         echo Unable to download ${ARCHIVE}
         exit 1
      fi
   fi

   [ -z "${ARCHIVER}" ] && return 0

   if [ ! -e "${SRC_DIR}" -o "${CLOBBER_SOURCE}" = "true" ]
   then
      rm -rf ${SRC_DIR}
      echo Extracting ${ARCHIVE}
      if [ "${MAKE_DIR}" = "true" ]
      then
         mkdir ${SRC_DIR}
         cd ${SRC_DIR}
         ${ARCHIVER} ../${ARCHIVE} > ../${ARCHIVE}.log 2>&1
      else
         ${ARCHIVER} ${ARCHIVE} > ${ARCHIVE}.log 2>&1
         cd ${SRC_DIR}
      fi
      return 0
   fi

   cd ${SRC_DIR}
   return 1
}

parse_output()
{
   sed -ne '/\\$/N' -e 's/\\\n//' -e 's/\t\+/ /g' -e 's/ \+/ /g' \
       -e '/ error: /p' \
       -e "s%.*Entering directory[ ]\\+.${DEPPKG_DIR}/\\([^ ]\+\).%Entering \\1%p" \
       -e "s%.*Leaving directory[ ]\\+.${DEPPKG_DIR}/\\([^ ]\+.\).%Leaving \\1%p" \
       -e '/gcc \|g\+\+ \|ar /!d' \
       -e 's/ \(\.\.\/\)\+/ /g' \
       -e 's/.* \([^ ]\+\(\.c\|\.cpp\|\.cc\|\.cxx\)\)\( .*\|\)$/Compiling \1/p' \
       -e 's/.* \([^ ]\+\.s\)\( .*\|\)$/Assembling \1/p' \
       -e 's/.*ar [^ ]\+ \([^ ]\+\)\(\( [^ ]\+\.o\)\+\)/Updating \1 -\2/p' \
       -e 's/.* -o \([^ ]\+\)\( .*\|\)$/Linking \1/p'
}

do_patch()
{
   PATCH_FILE=${SCRIPT_DIR}/patches/$1; shift
   
   if patch -f -p0 "$@" >>patch.log < ${PATCH_FILE}
   then
      :
   else
      echo "Patch failed - Check `pwd`/patch.log" > /dev/tty
      exit 1
   fi
}

do_make()
{
   if make -f "$@" 2>&1
   then
      :
   else
      echo "Make failed - Check `pwd`/make.log" > /dev/tty
      exit 1
   fi | tee -a make.log | parse_output
}

process_rsync()
{
   get_source "${URL_RSYNC}" "${DIR_RSYNC}" "${MKD_RSYNC}"
   ./configure --host=x86_64-pc-mingw32
   make -k # rdiff.exe has a problem
   cp .libs/librsync.a ${DEPPKG_DIR}/lib
   cp librsync*h ${DEPPKG_DIR}/include
}

process_zlib()
{
   if get_source "${URL_ZLIB}" "${DIR_ZLIB}" "${MKD_ZLIB}"
   then
      echo "Patching zlib"
      >patch.log
      do_patch zlib.patch
   fi
   echo Building zlib
   > make.log
   do_make win32/Makefile.mingw32 PREFIX=${DEPPKG_DIR} all
   echo Installing zlib
   do_make win32/Makefile.mingw32 PREFIX=${DEPPKG_DIR} install
}

process_pcre()
{
   if get_source "${URL_PCRE}" "${DIR_PCRE}" "${MKD_PCRE}"
   then
           echo Patching PCRE
           >patch.log
           do_patch pcre.patch
           echo Configuring PCRE
           ./configure CC_FOR_BUILD=gcc \
                       CXX_FOR_BUILD=g++ \
                       --host=x86_64-pc-mingw32 \
                       --prefix=${DEPPKG_DIR} \
                       --enable-utf8 \
                       --enable-unicode-properties >make.log 2>&1
   fi
   echo Building PCRE
   do_make Makefile PREFIX=${DEPPKG_DIR} all
   echo Installing PCRE
   do_make Makefile PREFIX=${DEPPKG_DIR} install
}

process_db()
{
   if get_source "${URL_DB}" "${DIR_DB}" "${MKD_DB}"
   then
          echo No Patch
   fi
   cd build_unix
   ../dist/configure --host=x86_64-pc-mingw32 --enable-mingw --prefix=${DEPPKG_DIR}
   > make.log
   echo Building DB
   do_make Makefile
   echo Installing DB
   do_make Makefile install_setup install_include install_lib
}

process_pthreads()
{
   if get_source "${URL_PTHREADS}" "${DIR_PTHREADS}" "${MKD_PTHREADS}"
   then
           echo Patching pthreads
           >patch.log
           do_patch pthreads-w64.patch
   fi
   echo Building pthreads
   > make.log
   do_make GNUmakefile GCE
   echo Installing pthreads
   rm -rf ${DEPPKG_DIR}/include/pthreads
   mkdir ${DEPPKG_DIR}/include/pthreads
   cp -p *.h ${DEPPKG_DIR}/include/pthreads
   cp -p *.dll ${DEPPKG_DIR}/bin
   cp -p *.a ${DEPPKG_DIR}/lib
}

process_openssl()
{
   if get_source "${URL_OPENSSL}" "${DIR_OPENSSL}" "${MKD_OPENSSL}"
   then
           echo Patching openssl
           >patch.log
           do_patch openssl-w64.patch
           echo Configuring openssl
           echo ./Configure --prefix=${DEPPKG_DIR} \
                       shared zlib-dynamic no-dso no-hw \
                       threads \
                       --with-zlib-include=${DEPPKG_DIR}/include \
                       mingw64
           ./Configure --prefix=${DEPPKG_DIR} \
                       shared zlib-dynamic no-dso no-hw \
                       threads \
                       --with-zlib-include=${DEPPKG_DIR}/include \
                       mingw64 > make.log 2>&1
   fi
   echo Building openssl
   perl util/mkdef.pl 32 libeay no-static-engine >ms/libeay32.def
   perl util/mkdef.pl 32 ssleay >ms/ssleay32.def
   perl util/mkdef.pl crypto ssl NT update
   sed -i 's/CC=/CC=mingw32-gcc #/' crypto/comp/Makefile
   sed -i 's/AR=/AR=mingw32-ar r #/'  crypto/comp/Makefile
   sed -i "s:^CFLAGS=:CFLAGS= -I${DEPPKG_DIR}/include:" crypto/comp/Makefile

   do_make Makefile all
   cd engines
   touch lib4758cca.bad libaep.bad libatalla.bad libcswift.bad libchil.bad libgmp.bad libnuron.bad libsureware.bad libubsec.bad libcapi.bad
   cd ..
   do_make Makefile all
   echo Installing openssl
   do_make Makefile install_sw install
   cp *.dll ${DEPPKG_DIR}/bin
}

if [ "$#" -eq 0 ]
then
   process_zlib
   process_rsync
   process_pcre
   process_pthreads
   process_openssl
else
   for dependency in "$@"
   do
      eval "process_${dependency}"
   done
fi
#vss
#Need to download from Microsoft
