#!/bin/bash
ARCHITECTURE=${1}

echo Architecture is ${ARCHITECTURE}

if [[ ${ARCHITECTURE} = "i386" || ${ARCHITECTURE} = "x86_64"  ]]; then
    TARGET="iossimulator-xcrun"
else
    TARGET="ios-cross"
    export CROSS_TOP="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer"
    export CROSS_SDK="iPhoneOS.sdk"
    export CROSS_COMPILE=`xcode-select --print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin/
    TOOLCHAIN_ROOT="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain"
fi

./Configure $TARGET "-arch $ARCHITECTURE -fembed-bitcode" no-asm no-shared no-hw no-async --prefix=$(pwd)/${ARCHITECTURE} || exit 1 

export PATH=${TOOLCHAIN_ROOT}/usr/bin:$PATH
make && make install || exit 2

lipo -info $(pwd)/${ARCHITECTURE}/lib/libcrypto.a
lipo -info $(pwd)/${ARCHITECTURE}/lib/libssl.a
lipo $(pwd)/${ARCHITECTURE}/lib/libcrypto.a -thin ${ARCHITECTURE} -output $(pwd)/${ARCHITECTURE}/lib/libcrypto.a.tmp
mv -f -v $(pwd)/${ARCHITECTURE}/lib/libcrypto.a.tmp $(pwd)/${ARCHITECTURE}/lib/libcrypto.a
lipo $(pwd)/${ARCHITECTURE}/lib/libssl.a -thin ${ARCHITECTURE} -output $(pwd)/${ARCHITECTURE}/lib/libssl.a.tmp
mv -f -v $(pwd)/${ARCHITECTURE}/lib/libssl.a.tmp $(pwd)/${ARCHITECTURE}/lib/libssl.a
lipo -info $(pwd)/${ARCHITECTURE}/lib/libcrypto.a
lipo -info $(pwd)/${ARCHITECTURE}/lib/libssl.a

TARGETARCHDIR=../../build
cp -r $(pwd)/${ARCHITECTURE} ${TARGETARCHDIR}



















echo buildplatform.sh done
