build.sh
#!/bin/bash

# This script builds openssl+libcurl libraries for MacOS, iOS and tvOS
#
# Jason Cox, @jasonacox
#   https://github.com/jasonacox/Build-OpenSSL-cURL
#

# Ensure we stop if build failure occurs
set -e

################################################
# EDIT this section to Select Default Versions #
################################################

OPENSSL="3.0.15"	# https://www.openssl.org/source/ - LTS Version
LIBCURL="8.11.1"	# https://curl.haxx.se/download.html
NGHTTP2="1.64.0"	# https://nghttp2.org/

################################################

# Build Machine
BUILD_MACHINE=`uname -m`
BUILD_CMD=$*

# Script Version
SCRIPT_VERSION="1.0.1"

# Compile Cache - Optional
# export CMAKE_CXX_COMPILER_LAUNCHER="ccache"

# Set minimum OS versions for target
MACOS_X86_64_VERSION=""			# Empty = use host version
MACOS_ARM64_VERSION=""			# Min supported is MacOS 11.0 Big Sur
CATALYST_IOS="15.0"				# Min supported is iOS 15.0 for Mac Catalyst
IOS_MIN_SDK_VERSION="8.0"
TVOS_MIN_SDK_VERSION="9.0"

# Semantic Version Comparison
version_lte() {
    [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}

if [ -z "${MACOS_X86_64_VERSION}" ]; then
	MACOS_X86_64_VERSION=$(sw_vers -productVersion)
fi
if [ -z "${MACOS_ARM64_VERSION}" ]; then
	MACOS_ARM64_VERSION=$(sw_vers -productVersion)
fi
if version_lte $MACOS_ARM64_VERSION 11.0; then
        MACOS_ARM64_VERSION="11.0"      # Min support for Apple Silicon is 11.0
fi

# Defaults
BUILDFOR="all"

# Global flags
engine=""
buildnghttp2="-n"
disablebitcode="-b"
colorflag=""
catalyst=""
sslv3=""
autobuild=""

# Formatting
default="\033[39m"
white="\033[97m"
green="\033[32m"
red="\033[91m"
yellow="\033[33m"

bold="\033[0m${white}\033[1m"
subbold="\033[0m${green}"
normal="${white}\033[0m"
dim="\033[0m${white}\033[2m"
alert="\033[0m${red}\033[1m"
alertdim="\033[0m${red}\033[2m"
archbold="\033[0m${yellow}\033[1m"

# Set trap to alert on error and show line
trap 'echo -e "${alert}** ERROR occurred on line $LINENO ($0) - exit code: $?"' ERR
trap 'echo -e "\n${alertdim}Cancelled by User${normal}"; exit 1' INT

# Show Usage
usage () {
	echo -e "${bold}Build-OpenSSL-cURL${dim} - v$SCRIPT_VERSION"
    echo
	echo -e "${bold}Usage:${normal}"
	echo
	echo -e "  ${subbold}$0${normal} [-o ${dim}<OpenSSL version>${normal}] [-c ${dim}<curl version>${normal}] [-n ${dim}<nghttp2 version>${normal}] [-d] [-e] [-3] [-x] [-h] [...]"
	echo
	echo "         -o <version>   Build OpenSSL version (default $OPENSSL)"
	echo "         -c <version>   Build curl version (default $LIBCURL)"
	echo "         -n <version>   Build nghttp2 version (default $NGHTTP2)"
	echo "         -d             Compile without HTTP2 support"
	echo "         -e             Compile with OpenSSL engine support"
	echo "         -b             Compile without bitcode (deprecated)"
	echo "         -m             Compile Mac Catalyst library"
	echo "         -u <version>   Mac Catalyst iOS min target version (default $CATALYST_IOS)"
	echo "         -3             Compile with SSLv3"
	echo "         -s <version>   iOS min target version (default $IOS_MIN_SDK_VERSION)"
	echo "         -t <version>   tvOS min target version (default $TVOS_MIN_SDK_VERSION)"
	echo "         -i <version>   macOS 86_64 min target version (default $MACOS_X86_64_VERSION)"
	echo "         -a <version>   macOS arm64 min target version (default $MACOS_ARM64_VERSION)"
	echo "         -x             No color output"
	echo "         -p <platform>  Build only for specified platform (iOS, tvOS, macOS, or all [default])"
	echo "         -y             Skip build confirmation"
	echo "         -h             Show usage"
	echo
    exit 127
}

# Process command line arguments
while getopts "o:c:n:u:s:t:i:a:p:debm3xhy\?" o; do
	case "${o}" in
		o) OPENSSL="${OPTARG}" ;;
		c) LIBCURL="${OPTARG}" ;;
		n) NGHTTP2="${OPTARG}" ;;
		d) buildnghttp2="" ;;
		e) engine="-e" ;;
		b) disablebitcode="-b" ;;
		m) catalyst="-m" ;;
		u) catalyst="-m -u ${OPTARG}"; CATALYST_IOS="${OPTARG}" ;;
		3) 
			echo "WARNING: SSLv3 is requested. SSLv3 is not secure and has been deprecated."
			echo "If you proceed, builds may fail as SSLv3 is no longer supported by curl."
			read -p "Do you want to continue (y/N)? " choice
			case "$choice" in 
				y|Y ) echo "Continuing with SSLv3 build"; echo "" ;;
				* ) echo "Exiting"; exit 1 ;;
			esac
			sslv3="-3"
			;;
		s) IOS_MIN_SDK_VERSION="${OPTARG}" ;;
		t) TVOS_MIN_SDK_VERSION="${OPTARG}" ;;
		i) MACOS_X86_64_VERSION="${OPTARG}" ;;
		a) MACOS_ARM64_VERSION="${OPTARG}" ;;
		x) 
			bold=""; subbold=""; normal=""; dim=""; alert=""; alertdim=""; archbold=""
			colorflag="-x"
			;;
		p) 
			BUILDFOR=$(echo "$OPTARG" | tr '[:upper:]' '[:lower:]')
			BUILDFORARGS="-p $BUILDFOR"
			if [ "$BUILDFOR" == "all" ]; then
				BUILDFORARGS=""
			elif [ "$BUILDFOR" != "ios" ] && [ "$BUILDFOR" != "tvos" ] && [ "$BUILDFOR" != "macos" ]; then
				echo -e "${alert}Invalid platform requested${normal}: $BUILDFOR"
				echo "Please specify iOS, tvOS or macOS"
				exit 127
			fi
			catalyst="" # Clear catalyst if not building for all
			;;
		y) autobuild="-y" ;;
		*) usage ;;
	esac
done
shift $((OPTIND-1))

# Set OS min versions
OSARGS="-s ${IOS_MIN_SDK_VERSION} -t ${TVOS_MIN_SDK_VERSION} -i ${MACOS_X86_64_VERSION} -a ${MACOS_ARM64_VERSION}"

## Welcome
echo -e "${bold}Build-OpenSSL-cURL${dim} - v$SCRIPT_VERSION"
echo
echo -e " - OpenSSL version: ${subbold}${OPENSSL}${dim}"
echo -e " - cURL version:    ${subbold}${LIBCURL}${dim}"
if [ "$buildnghttp2" == "" ]; then
	echo ""
	echo -n "This script builds OpenSSL and libcurl for "
else
	echo -e " - nghttp2 version: ${subbold}${NGHTTP2}${dim}"
	echo ""
	echo -n "This script builds OpenSSL, nghttp2 and libcurl for "
fi
case "$BUILDFOR" in
	ios) echo -e "${archbold}iOS${dim}" ;;
	tvos) echo -e "${archbold}tvOS${dim}" ;;
	macos) echo -e "${archbold}macOS${dim}" ;;
	*) 
	    if [ "$catalyst" != "" ]; then
			echo -e "${archbold}macOS${dim}, ${archbold}catalyst${dim}, ${archbold}iOS${dim} and ${archbold}tvOS${dim}"
		else
			echo -e "${archbold}macOS${dim}, ${archbold}iOS${dim} and ${archbold}tvOS${dim}"
		fi
		;;
esac

## Ask for confirmation to continue
if [ "$autobuild" == "" ]; then
	read -p "Continue (Y/n)? " choice
	case "$choice" in 
		n|N ) echo "Exiting"; exit 1 ;;
		* ) echo "Continuing..."; echo "" ;;
	esac
fi

## Start Counter
START=$(date +%s)

# Starting with OpenSSL 3.0 force nobitcode
if [[ "$OPENSSL" = "3.0"* ]]; then
	disablebitcode="-b"
fi

## OpenSSL Build
echo
cd openssl
echo -e "${bold}Building OpenSSL${normal}"
./openssl-build.sh -v "$OPENSSL" $engine $colorflag $catalyst $sslv3 $OSARGS $BUILDFORARGS
cd ..

## Nghttp2 Build
if [ "$buildnghttp2" == "" ]; then
	NGHTTP2="NONE"
else
	echo
	echo -e "${bold}Building nghttp2 for HTTP2 support${normal}"
	cd nghttp2
	./nghttp2-build.sh -v "$NGHTTP2" $colorflag $catalyst $OSARGS $BUILDFORARGS
	cd ..
fi

## Curl Build
echo
echo -e "${bold}Building Curl${normal}"
cd curl
./libcurl-build.sh -v "$LIBCURL" $disablebitcode $colorflag $buildnghttp2 $catalyst $sslv3 $OSARGS $BUILDFORARGS
cd ..

## Archive Libraries and Clean Up
echo
echo -e "${bold}Libraries...${normal}"
echo
echo -e "${subbold}openssl${normal} [${dim}$OPENSSL${normal}]${dim}"
xcrun -sdk iphoneos lipo -info openssl/*/lib/*.a
if [ "$buildnghttp2" != "" ]; then
	echo
	echo -e "${subbold}nghttp2 (rename to libnghttp2.a)${normal} [${dim}$NGHTTP2${normal}]${dim}"
	xcrun -sdk iphoneos lipo -info nghttp2/lib/*.a
fi
echo
echo -e "${subbold}libcurl (rename to libcurl.a)${normal} [${dim}$LIBCURL${normal}]${dim}"
xcrun -sdk iphoneos lipo -info curl/lib/*.a

ARCHIVE="archive/libcurl-$LIBCURL-openssl-$OPENSSL-nghttp2-$NGHTTP2"

echo
echo -e "${bold}Creating archive with XCFrameworks for release v$LIBCURL...${dim}"
echo "  See $ARCHIVE"
rm -rf "$ARCHIVE"
mkdir -p "$ARCHIVE"
mkdir -p "$ARCHIVE/include/openssl"
mkdir -p "$ARCHIVE/include/curl"
mkdir -p "$ARCHIVE/lib/iOS"
mkdir -p "$ARCHIVE/lib/iOS-simulator"
mkdir -p "$ARCHIVE/lib/iOS-fat"
mkdir -p "$ARCHIVE/lib/MacOS"
mkdir -p "$ARCHIVE/lib/tvOS"
mkdir -p "$ARCHIVE/lib/tvOS-simulator"
if [ "$catalyst" != "" ]; then
	mkdir -p "$ARCHIVE/lib/Catalyst"
fi
mkdir -p "$ARCHIVE/bin"
mkdir -p "$ARCHIVE/framework"
mkdir -p "$ARCHIVE/xcframework"

# libraries for libcurl, libcrypto and libssl
if [ "$BUILDFOR" == "ios" ] || [ "$BUILDFOR" == "all" ]; then
	# Copy iOS libraries
	cp curl/lib/libcurl_iOS.a $ARCHIVE/lib/iOS/libcurl.a
	cp curl/lib/libcurl_iOS-simulator.a $ARCHIVE/lib/iOS-simulator/libcurl.a
	cp curl/lib/libcurl_iOS-fat.a $ARCHIVE/lib/iOS-fat/libcurl.a
	cp openssl/iOS/lib/libcrypto.a $ARCHIVE/lib/iOS/libcrypto.a
	cp openssl/iOS-simulator/lib/libcrypto.a $ARCHIVE/lib/iOS-simulator/libcrypto.a
	cp openssl/iOS-fat/lib/libcrypto.a $ARCHIVE/lib/iOS-fat/libcrypto.a
	cp openssl/iOS/lib/libssl.a $ARCHIVE/lib/iOS/libssl.a
	cp openssl/iOS-simulator/lib/libssl.a $ARCHIVE/lib/iOS-simulator/libssl.a
	cp openssl/iOS-fat/lib/libssl.a $ARCHIVE/lib/iOS-fat/libssl.a
fi
if [ "$BUILDFOR" == "tvos" ] || [ "$BUILDFOR" == "all" ]; then
	# Copy tvOS libraries
	cp curl/lib/libcurl_tvOS.a $ARCHIVE/lib/tvOS/libcurl.a
	cp curl/lib/libcurl_tvOS-simulator.a $ARCHIVE/lib/tvOS-simulator/libcurl.a
	cp openssl/tvOS/lib/libcrypto.a $ARCHIVE/lib/tvOS/libcrypto.a
	cp openssl/tvOS-simulator/lib/libcrypto.a $ARCHIVE/lib/tvOS-simulator/libcrypto.a
	cp openssl/tvOS/lib/libssl.a $ARCHIVE/lib/tvOS/libssl.a
	cp openssl/tvOS-simulator/lib/libssl.a $ARCHIVE/lib/tvOS-simulator/libssl.a
fi
if [ "$BUILDFOR" == "macos" ] || [ "$BUILDFOR" == "all" ]; then
	# Copy MacOS libraries
	cp curl/lib/libcurl_Mac.a $ARCHIVE/lib/MacOS/libcurl.a
	cp openssl/Mac/lib/libcrypto.a $ARCHIVE/lib/MacOS/libcrypto.a
	cp openssl/Mac/lib/libssl.a $ARCHIVE/lib/MacOS/libssl.a
fi

if [ "$catalyst" != "" ]; then
	# Add catalyst libraries
	cp curl/lib/libcurl_Catalyst.a $ARCHIVE/lib/Catalyst/libcurl.a
	cp openssl/Catalyst/lib/libcrypto.a $ARCHIVE/lib/Catalyst/libcrypto.a
	cp openssl/Catalyst/lib/libssl.a $ARCHIVE/lib/Catalyst/libssl.a

	# Build XCFrameworks with Catalyst library
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/iOS-simulator/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/tvOS/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/Catalyst/libcurl.a \
        -headers curl/include \
        -library $ARCHIVE/lib/MacOS/libcurl.a \
        -headers curl/include \
		-output $ARCHIVE/xcframework/libcurl.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcrypto.a \
        -headers openssl/iOS/include \
		-library $ARCHIVE/lib/iOS-simulator/libcrypto.a \
        -headers openssl/iOS-simulator/include \
		-library $ARCHIVE/lib/tvOS/libcrypto.a \
        -headers openssl/tvOS/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcrypto.a \
        -headers openssl/tvOS-simulator/include \
		-library $ARCHIVE/lib/Catalyst/libcrypto.a \
        -headers openssl/Mac/include \
        -library $ARCHIVE/lib/MacOS/libcrypto.a \
        -headers openssl/Mac/include \
		-output $ARCHIVE/xcframework/libcrypto.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libssl.a \
		-library $ARCHIVE/lib/iOS-simulator/libssl.a \
		-library $ARCHIVE/lib/tvOS/libssl.a \
		-library $ARCHIVE/lib/tvOS-simulator/libssl.a \
		-library $ARCHIVE/lib/Catalyst/libssl.a \
        -library $ARCHIVE/lib/MacOS/libssl.a \
		-output $ARCHIVE/xcframework/libssl.xcframework
elif [ "$BUILDFOR" == "all" ]; then
	# Build XCFrameworks
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/iOS-simulator/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/tvOS/libcurl.a \
        -headers curl/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcurl.a \
        -headers curl/include \
        -library $ARCHIVE/lib/MacOS/libcurl.a \
        -headers curl/include \
		-output $ARCHIVE/xcframework/libcurl.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcrypto.a \
        -headers openssl/iOS/include \
		-library $ARCHIVE/lib/iOS-simulator/libcrypto.a \
        -headers openssl/iOS-simulator/include \
		-library $ARCHIVE/lib/tvOS/libcrypto.a \
        -headers openssl/tvOS/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcrypto.a \
        -headers openssl/tvOS-simulator/include \
        -library $ARCHIVE/lib/MacOS/libcrypto.a \
        -headers openssl/Mac/include \
		-output $ARCHIVE/xcframework/libcrypto.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libssl.a \
		-library $ARCHIVE/lib/iOS-simulator/libssl.a \
		-library $ARCHIVE/lib/tvOS/libssl.a \
		-library $ARCHIVE/lib/tvOS-simulator/libssl.a \
        -library $ARCHIVE/lib/MacOS/libssl.a \
		-output $ARCHIVE/xcframework/libssl.xcframework
	# openssl/openssl-ios-armv7_armv7s_arm64_arm64e.a   
	# openssl/openssl-ios-i386_x86_64_arm64-simulator.a
	cp openssl/*.a $ARCHIVE/framework
elif [ "$BUILDFOR" == "ios" ]; then
	# Build XCFrameworks
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcurl.a \
		-headers curl/include \
		-library $ARCHIVE/lib/iOS-simulator/libcurl.a \
		-headers curl/include \
		-output $ARCHIVE/xcframework/libcurl.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libcrypto.a \
		-headers openssl/iOS/include \
		-library $ARCHIVE/lib/iOS-simulator/libcrypto.a \
		-headers openssl/iOS-simulator/include \
		-output $ARCHIVE/xcframework/libcrypto.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/iOS/libssl.a \
		-library $ARCHIVE/lib/iOS-simulator/libssl.a \
		-output $ARCHIVE/xcframework/libssl.xcframework
	# openssl/openssl-ios-armv7_armv7s_arm64_arm64e.a   
	# openssl/openssl-ios-i386_x86_64_arm64-simulator.a
	cp openssl/*.a $ARCHIVE/framework
elif [ "$BUILDFOR" == "tvos" ]; then
	# Build XCFrameworks
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/tvOS/libcurl.a \
		-headers curl/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcurl.a \
		-headers curl/include \
		-output $ARCHIVE/xcframework/libcurl.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/tvOS/libcrypto.a \
		-headers openssl/tvOS/include \
		-library $ARCHIVE/lib/tvOS-simulator/libcrypto.a \
		-headers openssl/tvOS-simulator/include \
		-output $ARCHIVE/xcframework/libcrypto.xcframework
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/tvOS/libssl.a \
		-library $ARCHIVE/lib/tvOS-simulator/libssl.a \
		-output $ARCHIVE/xcframework/libssl.xcframework
elif [ "$BUILDFOR" == "macos" ]; then
	# Build XCFrameworks
	xcodebuild -create-xcframework \
		-library $ARCHIVE/lib/MacOS/libcurl.a \
		-headers curl/include \
		-output $ARCHIVE/xcframework/libcurl.xcframework
fi

# libraries for nghttp2
if [ "$buildnghttp2" != "" ]; then
    # nghttp2 libraries 
	if [ "$BUILDFOR" == "ios" ] || [ "$BUILDFOR" == "all" ]; then
		cp nghttp2/lib/libnghttp2_iOS.a $ARCHIVE/lib/iOS/libnghttp2.a
		cp nghttp2/lib/libnghttp2_iOS-simulator.a $ARCHIVE/lib/iOS-simulator/libnghttp2.a
		cp nghttp2/lib/libnghttp2_iOS-fat.a $ARCHIVE/lib/iOS-fat/libnghttp2.a
	fi
	if [ "$BUILDFOR" == "tvos" ] || [ "$BUILDFOR" == "all" ]; then
		cp nghttp2/lib/libnghttp2_tvOS.a $ARCHIVE/lib/tvOS/libnghttp2.a
		cp nghttp2/lib/libnghttp2_tvOS-simulator.a $ARCHIVE/lib/tvOS-simulator/libnghttp2.a
	fi
	if [ "$BUILDFOR" == "macos" ] || [ "$BUILDFOR" == "all" ]; then
		cp nghttp2/lib/libnghttp2_Mac.a $ARCHIVE/lib/MacOS/libnghttp2.a
	fi
	if [ "$catalyst" != "" ]; then
		cp nghttp2/lib/libnghttp2_Catalyst.a $ARCHIVE/lib/Catalyst/libnghttp2.a
		xcodebuild -create-xcframework \
			-library $ARCHIVE/lib/iOS/libnghttp2.a \
			-library $ARCHIVE/lib/iOS-simulator/libnghttp2.a \
			-library $ARCHIVE/lib/tvOS/libnghttp2.a \
			-library $ARCHIVE/lib/tvOS-simulator/libnghttp2.a \
			-library $ARCHIVE/lib/Catalyst/libnghttp2.a \
            -library $ARCHIVE/lib/MacOS/libnghttp2.a \
			-output $ARCHIVE/xcframework/libnghttp2.xcframework
	elif [ "$BUILDFOR" == "macos" ]; then
		xcodebuild -create-xcframework \
			-library $ARCHIVE/lib/MacOS/libnghttp2.a \
			-output $ARCHIVE/xcframework/libnghttp2.xcframework
	elif [ "$BUILDFOR" == "ios" ]; then
		xcodebuild -create-xcframework \
			-library $ARCHIVE/lib/iOS/libnghttp2.a \
			-library $ARCHIVE/lib/iOS-simulator/libnghttp2.a \
			-output $ARCHIVE/xcframework/libnghttp2.xcframework
	elif [ "$BUILDFOR" == "tvos" ]; then
		xcodebuild -create-xcframework \
			-library $ARCHIVE/lib/tvOS/libnghttp2.a \
			-library $ARCHIVE/lib/tvOS-simulator/libnghttp2.a \
			-output $ARCHIVE/xcframework/libnghttp2.xcframework
	else # all
		xcodebuild -create-xcframework \
			-library $ARCHIVE/lib/iOS/libnghttp2.a \
			-library $ARCHIVE/lib/iOS-simulator/libnghttp2.a \
			-library $ARCHIVE/lib/tvOS/libnghttp2.a \
			-library $ARCHIVE/lib/tvOS-simulator/libnghttp2.a \
            -library $ARCHIVE/lib/MacOS/libnghttp2.a \
			-output $ARCHIVE/xcframework/libnghttp2.xcframework
	fi
fi

# archive header files
if [ "$BUILDFOR" == "ios" ] || [  "$BUILDFOR" == "all" ]; then
	cp openssl/iOS/include/openssl/* "$ARCHIVE/include/openssl"
elif [ "$BUILDFOR" == "tvos" ]; then
	cp openssl/tvOS/include/openssl/* "$ARCHIVE/include/openssl"
elif [ "$BUILDFOR" == "macos" ]; then
	cp openssl/Mac/include/openssl/* "$ARCHIVE/include/openssl"
fi
cp curl/include/curl/* "$ARCHIVE/include/curl"

# grab root certs
curl -sL https://curl.se/ca/cacert.pem > $ARCHIVE/cacert.pem

# create README for archive
sed -e "s/ZZZCMDS/$BUILD_CMD/g" -e "s/ZZZLIBCURL/$LIBCURL/g" -e "s/ZZZOPENSSL/$OPENSSL/g" -e "s/ZZZNGHTTP2/$NGHTTP2/g" archive/release-template.md > $ARCHIVE/README.md
echo

# EXAMPLE iOS and tvOS App - update test app with latest includes and XCFrameworks
echo -e "${bold}Copying libraries to Test Apps ...${dim}"
if [[ "$BUILDFOR" == "all" ]]; then
	EXAMPLES=("example/iOS Test App" "example/tvOS Test App" "example/macOS Test App")
elif [[ "$BUILDFOR" == "ios" ]]; then
	EXAMPLES=("example/iOS Test App")
elif [[ "$BUILDFOR" == "tvos" ]]; then
	EXAMPLES=("example/tvOS Test App")
elif [[ "$BUILDFOR" == "macos" ]]; then
	EXAMPLES=("example/macOS Test App")
fi
for EXAMPLE in "${EXAMPLES[@]}"; do
	echo "  Upating: $EXAMPLE"
	# Create necessary directories
	mkdir -p "$EXAMPLE/libs" "$EXAMPLE/include/curl" "$EXAMPLE/include/openssl"
	# Copy includes
	if [[ "$EXAMPLE" == *"macOS"* ]]; then
		cp openssl/Mac/include/openssl/* "$EXAMPLE/include/openssl/"
	elif [[ "$EXAMPLE" == *"iOS"* ]]; then
		cp openssl/iOS/include/openssl/* "$EXAMPLE/include/openssl/"
	elif [[ "$EXAMPLE" == *"tvOS"* ]]; then
		cp openssl/tvOS/include/openssl/* "$EXAMPLE/include/openssl/"
	fi
	cp curl/include/curl/* "$EXAMPLE/include/curl/"
	# Copy certificate bundle
	cp $ARCHIVE/cacert.pem "$EXAMPLE/cacert.pem"
	# Copy XCFrameworks
	cp -R $ARCHIVE/xcframework/*.xcframework "$EXAMPLE/libs/"
done
echo

# Create universal Mac binaries and run test
if [ "$BUILDFOR" == "macos" ] || [ "$BUILDFOR" == "all" ]; then	
	echo -e "${bold}Archiving Mac binaries for curl and openssl...${dim}"
	echo "  See $ARCHIVE/bin"
	lipo -create -output $ARCHIVE/bin/curl /tmp/curl-x86_64 /tmp/curl-arm64
	mv /tmp/curl-* $ARCHIVE/bin
	lipo -create -output $ARCHIVE/bin/openssl /tmp/openssl-x86_64 /tmp/openssl-arm64
	mv /tmp/openssl-* $ARCHIVE/bin
	echo
	echo -e "${bold}Testing Universal Mac binaries for ${BUILD_MACHINE}...${dim}"
	echo -e "  ${bold}cURL${dim}"
	file $ARCHIVE/bin/curl
	$ARCHIVE/bin/curl -V
	echo -e "  ${bold}OpenSSL${dim}"
	file $ARCHIVE/bin/openssl
	$ARCHIVE/bin/openssl version
fi

## Done - Display Build Duration
echo
echo -e "${bold}Build Complete${dim}"
date "+  %c - End"
END=$(date +%s)
secs=$(echo "$END - $START" | bc)
printf '  Duration %02dh:%02dm:%02ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))
echo -e "${normal}"

rm -f $NOHTTP2
