#!/bin/sh

source ../Config.sh

PP_PUBLIC_INCLUDES="include/openssl"

fake_includes () {
    for i in idea rc5 engine cast blowfish ripemd mdc2 ec ecdh ecdsa ocsp ui ui_compat; do
	touch include/openssl/$i.h
    done
}

pp_clean_local () {
    pp_clean_default
    rm -f Makefile include/openssl/*
    # sometimes clean fails and old object files lay around which lead to
    # strange behavior sometimes
    find -name "*.o" -exec rm -f {} \;
}

pp_build_local () {
    # force configure
    if [ "$PP_FORCE_FULL_BUILD" = "1" ]; then
	rm -f Makefile
    fi

    # configure
    if [ ! -f Makefile ]; then
	echo "Running configure ..."

	# no error strings in final
	if [ "$PP_BUILD_TYPE" = "final" ]; then
	    NO_ERR=no-err
	else
	    NO_ERR=
	fi

	# determine Configure target arch
	if [ "$PP_BUILD_ARCH" = "powerpc" ]; then
	    OPENSSL_ARCH=linux-ppc
	elif [ "$PP_BUILD_ARCH" = "arm" ]; then
	    OPENSSL_ARCH=linux-arm
	elif [ "$PP_BUILD_ARCH" = "x86" ]; then
	    OPENSSL_ARCH=linux-elf
	else
	    echo "Unknown PP_BUILD_ARCH: '$PP_BUILD_ARCH'"
	    return 1
	fi
    
        # skip "no-deprecated", we need it for open-ldap with tls!
	COMMON_OPTIONS="enable-rsa no-idea no-rc5 no-engine no-ssl2 no-dso no-ocsp no-cast no-ec no-ecdh no-ecdsa no-bf no-krb5 no-ripemd no-mdc2 no-ui no-camellia $NO_ERR"

	# Can we use the same dir for all? (probably /etc/pki/... as in FC5)
	[ "${PP_FEAT_STANDALONE_VIEWER}" = "1" ] \
	    && OPENSSLDIR=/etc/ssl \
	    || OPENSSLDIR=/etc/e-RIC/ssl 

	./Configure $OPENSSL_ARCH $COMMON_OPTIONS --openssldir="$OPENSSLDIR"
	fake_includes
    fi

    # make / install
    echo "Running make ..."
    make all fw-install
}

pp_do "$@"
