Skip to content

Get Libraries Script

The following script will download and install OpenSSL, Curl, Zlib, and nlohmann json libraries to install prefix.

#!/bin/bash

# TODO: Replace company
INSTALL_PREFIX="/opt/<company>/api/usr"

# Display help message
show_help() {
    echo "Usage: $0 [options]"
    echo ""
    echo "Options:"
    echo "  -h, --help          Show this help message and exit."
    echo "  -p, --prefix PREFIX Set the installation prefix. Default is /opt/<company>/api/usr."
    echo ""
}

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        -p|--prefix)
            INSTALL_PREFIX="$2"
            shift # past argument
            shift # past value
            ;;
        *)    # unknown option
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

JSONLIB_VERSION="3.6.1"
CURL_VERSION="7.64.1"
ZLIB_VERSION="1.2.11"
OPENSSL_VERSION="1.1.1g"

download_if_not_exist() {
    if [ ! -d "$1" ]; then
        echo "Downloading $2 from $3"
        curl -L "$3" -o "$2.tar.gz"
        tar xf "$2.tar.gz"
        rm -rf "$2.tar.gz"
    else
        echo "$2 already exists, skipping download."
    fi
}

get_nproc() {
    echo $(nproc)
}

create_prefix() {
    if [ -d "${INSTALL_PREFIX}" ]; then
        echo "Directory ${INSTALL_PREFIX} already exists, skipping creation."
    else
        echo "Creating installation prefix directory at ${INSTALL_PREFIX}..."
        if ! mkdir -p "${INSTALL_PREFIX}"; then
            echo "Error: Failed to create directory at ${INSTALL_PREFIX}."
            exit 1
        fi
        echo "${INSTALL_PREFIX} directory created successfully."
    fi
}

install_zlib() {
    echo "Starting installation of zlib..."
    download_if_not_exist "zlib-${ZLIB_VERSION}" "zlib" "https://ftp.osuosl.org/pub/libpng/src/zlib/zlib-${ZLIB_VERSION}.tar.gz"
    cd "zlib-${ZLIB_VERSION}"
    echo "Configuring zlib..."
    ./configure --prefix="${INSTALL_PREFIX}" --static
    echo "Building zlib..."
    make -j $(get_nproc) --trace
    echo "Installing zlib..."
    make install
    cd ..
    echo "zlib installation complete."
}

install_openssl() {
    echo "Starting installation of OpenSSL..."
    download_if_not_exist "openssl-${OPENSSL_VERSION}" "openssl" "https://www.openssl.org/source/old/1.1.1/openssl-${OPENSSL_VERSION}.tar.gz"
    cd "openssl-${OPENSSL_VERSION}"
    echo "Configuring OpenSSL..."
    ./Configure linux-x86_64 \
        --prefix="${INSTALL_PREFIX}" \
        --api=1.0.0 \
        --with-zlib-lib="${INSTALL_PREFIX}/lib" \
        --with-zlib-include="${INSTALL_PREFIX}/include" \
        -DOPENSSL_SMALL_FOOTPRINT \
        -static 
    echo "Building OpenSSL..."
    make -j $(get_nproc) build_libs
    echo "Installing OpenSSL..."
    make install_dev
    cd ..
    echo "OpenSSL installation complete."
}

install_curl() {
    echo "Starting installation of Curl..."
    download_if_not_exist "curl-${CURL_VERSION}" "curl" "https://curl.se/download/curl-${CURL_VERSION}.tar.gz"
    cd "curl-${CURL_VERSION}"
    echo "Configuring Curl..."
    ./configure \
        --prefix="${INSTALL_PREFIX}" \
        --disable-alt-svc \
        --disable-ares \
        --disable-cookies \
        --disable-dict \
        --disable-file \
        --disable-ftp \
        --disable-gopher \
        --disable-imap \
        --disable-ipv6 \
        --disable-ldap \
        --disable-ldaps \
        --disable-manual \
        --disable-ntlm-wb \
        --disable-pop3 \
        --disable-proxy \
        --disable-smb \
        --disable-smtp \
        --disable-sspi \
        --disable-telnet \
        --disable-test \
        --disable-tftp \
        --disable-tls-srp \
        --disable-unix-sockets \
        --disable-verbose \
        --disable-versioned-symbols \
        --disable-shared \
        --enable-crypto-auth \
        --enable-rtsp \
        --enable-http \
        --enable-libcurl-option \
        --enable-verbose \
        --enable-static \
        --with-zlib="${INSTALL_PREFIX}" \
        --with-ssl="${INSTALL_PREFIX}" \
        --without-nss \
        --without-wolfssl \
        --without-libidn \
        --without-librtmp \
        --without-ca-bundle \
        --without-ca-path \
        --without-ca-fallback 
    echo "Building Curl..."
    make -j $(get_nproc) --trace
    echo "Installing Curl..."
    make install
    cd ..
    echo "Curl installation complete."
}

install_json() {
    echo "Starting installation of nlohmann/json..."
    download_if_not_exist "json-${JSONLIB_VERSION}" "json" "https://github.com/nlohmann/json/archive/refs/tags/v${JSONLIB_VERSION}.tar.gz"
    cd "json-${JSONLIB_VERSION}"
    cmake --build build/ --target clean
    cmake -S . -B build/ -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX="${INSTALL_PREFIX}" -DCMAKE_BUILD_TYPE=Release
    cmake --build build/ --target install
    cd ..
    echo "nlohmann/json installation complete."
}

create_prefix
install_zlib
install_openssl
install_curl
install_json