#!/bin/sh
# This script is meant to help build zsign on systems, such as Ubuntu 20.04 LTS
# where only openssl1.1 are available. This script also supports MacOS hosts
# with brew installations of openssl1.1, though newer versions of openssl should
# now be supported as well via the main script build_zsign.sh

SEDOPTION="-i"
case "$OSTYPE"  in
  "darwin"*) SEDOPTION="-i ''"
esac

if [ -e zsign.src ]; then
    echo "Directory exists: zsign.src"
    echo "Remove directory and try again"
    exit 1
fi

if [ -e zsign ]; then
    echo "File 'zsign' already exists in current directory."
    echo "Delete file and try again."
    exit 1
fi

git clone https://github.com/zhlynn/zsign zsign.src
cd zsign.src

# The small commit that follows fixes compilation against modern openssl, while
# breaking compilation against openssl1.1
git checkout ee985e67943285fd94b9995bc4b1f87dece23440

if [ ! -e CMakeLists.txt.bak ]; then
    cp CMakeLists.txt CMakeLists.txt.bak
    # See https://github.com/zhlynn/zsign/issues/208
    # Reported as fixed on MacOS, but not fixed.
    sed $SEDOPTION 's/COMMAND brew --prefix OpenSSL *$/COMMAND brew --prefix OpenSSL@1.1/' CMakeLists.txt
fi

mkdir build
cd build
cmake ../
make

cp zsign ../..

