#!/bin/bash

OPEN_SSL_VER="1.1.1i"
PM_NET_SSLEAY_VER="1.88"
PM_CRYPT_SSLEAY_VER="0.72"

DST_ROOT="/opt/thirdlane"

BUILD_ROOT="/tmp"
CPAN_MIRROR="https://cpan.metacpan.org"

#================================================================================
PM_NET_SSLEAY_SRC_NAME="Net-SSLeay-${PM_NET_SSLEAY_VER}"
PM_NET_SSLEAY_SRC="${PM_NET_SSLEAY_SRC_NAME}.tar.gz"
PM_NET_SSLEAY_URL="${CPAN_MIRROR}/authors/id/C/CH/CHRISN/${PM_NET_SSLEAY_SRC}"

PM_CRYPT_SSLEAY_SRC_NAME="Crypt-SSLeay-${PM_CRYPT_SSLEAY_VER}"
PM_CRYPT_SSLEAY_SRC="${PM_CRYPT_SSLEAY_SRC_NAME}.tar.gz"
PM_CRYPT_SSLEAY_URL="${CPAN_MIRROR}/authors/id/N/NA/NANIS/${PM_CRYPT_SSLEAY_SRC}"

OPEN_SSL_SRC_NAME="openssl-${OPEN_SSL_VER}"
OPEN_SSL_SRC="${OPEN_SSL_SRC_NAME}.tar.gz"
OPEN_SSL_URL="https://www.openssl.org/source/${OPEN_SSL_SRC}"
OPEN_SSL_DST_ROOT="${DST_ROOT}/perl_lib/local/usr"

MACHINE=`(uname -m) 2>/dev/null`

OPEN_SSL_BUILD_OPTS+=" no-asm"
OPEN_SSL_BUILD_OPTS+=" threads"
OPEN_SSL_BUILD_OPTS+=" shared"
OPEN_SSL_BUILD_OPTS+=" linux-${MACHINE}"

BUILD_TEMP="${BUILD_ROOT}/_${OPEN_SSL_SRC_NAME}_build"

mkdir -p "${BUILD_TEMP}"
mkdir -p "${OPEN_SSL_DST_ROOT}"

curl --url "${OPEN_SSL_URL}" -o "${BUILD_TEMP}/${OPEN_SSL_SRC}"

if [ -f "${BUILD_TEMP}/${OPEN_SSL_SRC}" ]; then
    (cd "${BUILD_TEMP}" && (tar -zxvf "${OPEN_SSL_SRC}"))
    if [ -d "${BUILD_TEMP}/${OPEN_SSL_SRC_NAME}" ]; then
	(cd "${BUILD_TEMP}/${OPEN_SSL_SRC_NAME}" && (./Configure --prefix=${OPEN_SSL_DST_ROOT} --openssldir=${OPEN_SSL_DST_ROOT} ${OPEN_SSL_BUILD_OPTS} && make -j8 depend && make -j8 all && make install))
	echo "Installed ${OPEN_SSL_SRC_NAME}"
    else
	echo "Broken archive: ${BUILD_TEMP}/${OPEN_SSL_SRC}"
	exit 1
    fi
else
    echo "File not found: ${OPEN_SSL_URL}"
    exit 1
fi

curl --url "${PM_NET_SSLEAY_URL}" -o "${BUILD_TEMP}/${PM_NET_SSLEAY_SRC}"

if [ -f "${BUILD_TEMP}/${PM_NET_SSLEAY_SRC}" ]; then
    (cd "${BUILD_TEMP}" && (tar -zxvf "${PM_NET_SSLEAY_SRC}"))
    if [ -d "${BUILD_TEMP}/${PM_NET_SSLEAY_SRC_NAME}" ]; then
	(cd "${BUILD_TEMP}/${PM_NET_SSLEAY_SRC_NAME}" && (OPENSSL_PREFIX="${OPEN_SSL_DST_ROOT}" perl ./Makefile.PL PREFIX="${DST_ROOT}/perl_lib/local/over" && make && make install))
	echo "Installed ${PM_NET_SSLEAY_SRC_NAME}"
    else
	echo "Broken archive: ${BUILD_TEMP}/${PM_NET_SSLEAY_SRC}"
	exit 1
    fi
else
    echo "File not found: ${PM_NET_SSLEAY_URL}"
    exit 1
fi

curl --url "${PM_CRYPT_SSLEAY_URL}" -o "${BUILD_TEMP}/${PM_CRYPT_SSLEAY_SRC}"

if [ -f "${BUILD_TEMP}/${PM_CRYPT_SSLEAY_SRC}" ]; then
    (cd "${BUILD_TEMP}" && (tar -zxvf "${PM_CRYPT_SSLEAY_SRC}"))
    if [ -d "${BUILD_TEMP}/${PM_CRYPT_SSLEAY_SRC_NAME}" ]; then
	(cd "${BUILD_TEMP}/${PM_CRYPT_SSLEAY_SRC_NAME}" && (perl Makefile.PL --incpath="${OPEN_SSL_DST_ROOT}/include" --libpath="${OPEN_SSL_DST_ROOT}/lib" PREFIX="${DST_ROOT}/perl_lib/local/over"))
	echo "Installed ${PM_CRYPT_SSLEAY_SRC_NAME}"
    else
	echo "Broken archive: ${BUILD_TEMP}/${PM_CRYPT_SSLEAY_SRC}"
	exit 1
    fi
else
    echo "File not found: ${PM_CRYPT_SSLEAY_URL}"
    exit 1
fi

rm -rf "${BUILD_TEMP}"
