# packages/openssl/auxbuild
#

# download and unpack tarball
TARVERSION="1.0.1g"
TARDIR="${BUILD_PACKAGE}-${TARVERSION}"
TARBALL="${TARDIR}.tar.gz"

build_download "http://www.openssl.org/source/${TARBALL}"
build_unpack "${TARBALL}"

# apply patches matching pattern (repository, patch prefix)
build_patch "${TARDIR}" "${BUILD_PACKAGE}-ALL-"
build_patch "${TARDIR}" "${BUILD_PACKAGE}-${TARVERSION}-"

case "${BUILD_MACHINE}" in
CMG-NAM|CMG-NAM-mk2)
    os_compiler="linux-elf"
    ssl_cflags="-DL_ENDIAN -DTERMIO ${ARCH_CFLAGS}"
;;
CMG-DCM-*|CMG-DAS)
    os_compiler="linux-generic32"
    ssl_cflags="-DTERMIO ${ARCH_CFLAGS}"
;;
*)
    echo "Unknown BUILD_MACHINE"
    exit 1
;;
esac

cd "Distn/${TARDIR}"

sed -e 's|^my .cflags = .*$|my \$cflags = "'"${ssl_cflags}"'";|' -i Configure
# Unset any existing CC as Configure is dumb about creating a new one with
# --cross-compile-prefix= which we need for other commands.
unset CC
./Configure shared zlib-dynamic no-hw --prefix="/usr" \
    --openssldir="/etc/ssl" --install_prefix="${BUILDROOT}" \
    --cross-compile-prefix="${ARCH_CC_PREFIX}" -I${CROSSLIB}/include \
    -L${CROSSLIB}/lib -lz \
    -no-ec \
    ${os_compiler}

# Disable manpage build/install
sed -e '/^install:/s:install_docs::' -i Makefile Makefile.org

# Fix "Makefile is older than …" detection
sleep 1
touch Makefile

make clean
make
make install

rm -rf "${BUILDROOT}/etc/ssl/misc"

true
# vim: ts=4:sw=4:expandtab:syntax=sh
