#!/bin/bash
#
# Copyright (c) 2017 Ollix
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# ---
# Author: olliwang@ollix.com (Olli Wang)
#
# This script compiles the metal source file "nanovg_mtl_shaders.metal"
# into metallib binary data as defined in "nanovg_mtl_metallib_*.h" headers.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/../src

result=true

PREFIX="mnvg_bitcode"

mkdir $PREFIX

execute_cmd() {
  SDK=$1
  TARGET=$2

  SUFFIX=""
  CMD="xcrun -sdk $SDK metal -c -frecord-sources=flat nanovg_mtl_shaders.metal"

  AIR_FILE_NAME="$PREFIX/$TARGET$SUFFIX.air"
  HEADER_NAME="$PREFIX/$TARGET$SUFFIX.h"
  VARIABLE_NAME="$PREFIX/$TARGET$SUFFIX"

  CMD="$CMD -o $AIR_FILE_NAME -mmacosx-version-min=10.11"
  eval $CMD

  if [ $? -ne 0 ]; then
    return 1
  fi
  xcrun -sdk $SDK metal -frecord-sources -o $VARIABLE_NAME $AIR_FILE_NAME\
      > /dev/null 2>&1
  xcrun -sdk macosx metal-dsymutil -flat -remove-source $VARIABLE_NAME
  xxd -i $VARIABLE_NAME > $HEADER_NAME
  rm $AIR_FILE_NAME $VARIABLE_NAME
  return 0
}

echo "* Compiling debug shader for macOS..."
TARGET="macos"
execute_cmd "macosx" "macos"
if [ $? -ne 0 ]; then
  result=false
fi

if $result; then
  echo "Done"
fi
