#!/bin/bash

if [[ -z ${ARCH} ]]; then
  echo -e "\n(*) ARCH not defined\n"
  exit 1
fi

if [[ -z ${IOS_MIN_VERSION} ]]; then
  echo -e "\n(*) IOS_MIN_VERSION not defined\n"
  exit 1
fi

if [[ -z ${BASEDIR} ]]; then
  echo -e "\n(*) BASEDIR not defined\n"
  exit 1
fi

if [[ -z ${SDK_PATH} ]]; then
  echo -e "\n(*) SDK_PATH not defined\n"
  exit 1
fi

# ENABLE COMMON FUNCTIONS
source "${BASEDIR}"/scripts/function-"${FFMPEG_KIT_BUILD_TYPE}".sh 1>>"${BASEDIR}"/build.log 2>&1 || exit 1

echo -e "\nBuilding ${ARCH} platform\n"
echo -e "\nINFO: Starting new build for ${ARCH} at $(date)\n" 1>>"${BASEDIR}"/build.log 2>&1

# SET BASE INSTALLATION DIRECTORY FOR THIS ARCHITECTURE
export LIB_INSTALL_BASE="${BASEDIR}/prebuilt/$(get_build_directory)"

# CREATE PACKAGE CONFIG DIRECTORY FOR THIS ARCHITECTURE
PKG_CONFIG_DIRECTORY="${LIB_INSTALL_BASE}/pkgconfig"
if [ ! -d "${PKG_CONFIG_DIRECTORY}" ]; then
  mkdir -p "${PKG_CONFIG_DIRECTORY}" || exit 1
fi

# FILTER WHICH EXTERNAL LIBRARIES WILL BE BUILT
# NOTE THAT BUILT-IN LIBRARIES ARE FORWARDED TO FFMPEG SCRIPT WITHOUT ANY PROCESSING
enabled_library_list=()
for library in {1..47}; do
  if [[ ${!library} -eq 1 ]]; then
    ENABLED_LIBRARY=$(get_library_name $((library - 1)))
    enabled_library_list+=(${ENABLED_LIBRARY})

    echo -e "INFO: Enabled library ${ENABLED_LIBRARY} will be built\n" 1>>"${BASEDIR}"/build.log 2>&1
  fi
done

# BUILD ENABLED LIBRARIES AND THEIR DEPENDENCIES
let completed=0
while [ ${#enabled_library_list[@]} -gt $completed ]; do
  for library in "${enabled_library_list[@]}"; do
    let run=0
    case $library in
    fontconfig)
      if [[ $OK_expat -eq 1 ]] && [[ $OK_freetype -eq 1 ]]; then
        run=1
      fi
      ;;
    freetype)
      if [[ $OK_libpng -eq 1 ]]; then
        run=1
      fi
      ;;
    gnutls)
      if [[ $OK_nettle -eq 1 ]] && [[ $OK_gmp -eq 1 ]]; then
        run=1
      fi
      ;;
    harfbuzz)
      if [[ $OK_fontconfig -eq 1 ]] && [[ $OK_freetype -eq 1 ]]; then
        run=1
      fi
      ;;
    leptonica)
      if [[ $OK_giflib -eq 1 ]] && [[ $OK_jpeg -eq 1 ]] && [[ $OK_libpng -eq 1 ]] && [[ $OK_tiff -eq 1 ]] && [[ $OK_libwebp -eq 1 ]]; then
        run=1
      fi
      ;;
    libass)
      if [[ $OK_expat -eq 1 ]] && [[ $OK_freetype -eq 1 ]] && [[ $OK_fribidi -eq 1 ]] && [[ $OK_fontconfig -eq 1 ]] && [[ $OK_libpng -eq 1 ]] && [[ $OK_harfbuzz -eq 1 ]]; then
        run=1
      fi
      ;;
    libtheora)
      if [[ $OK_libvorbis -eq 1 ]] && [[ $OK_libogg -eq 1 ]]; then
        run=1
      fi
      ;;
    libvorbis)
      if [[ $OK_libogg -eq 1 ]]; then
        run=1
      fi
      ;;
    libwebp)
      if [[ $OK_giflib -eq 1 ]] && [[ $OK_jpeg -eq 1 ]] && [[ $OK_libpng -eq 1 ]] && [[ $OK_tiff -eq 1 ]]; then
        run=1
      fi
      ;;
    nettle)
      if [[ $OK_gmp -eq 1 ]]; then
        run=1
      fi
      ;;
    rubberband)
      if [[ $OK_libsndfile -eq 1 ]] && [[ $OK_libsamplerate -eq 1 ]]; then
        run=1
      fi
      ;;
    tesseract)
      if [[ $OK_leptonica -eq 1 ]]; then
        run=1
      fi
      ;;
    tiff)
      if [[ $OK_jpeg -eq 1 ]]; then
        run=1
      fi
      ;;
    twolame)
      if [[ $OK_libsndfile -eq 1 ]]; then
        run=1
      fi
      ;;
    *)
      run=1
      ;;
    esac

    # DEFINE SOME FLAGS TO MANAGE DEPENDENCIES AND REBUILD OPTIONS
    BUILD_COMPLETED_FLAG=$(echo "OK_${library}" | sed "s/\-/\_/g")
    REBUILD_FLAG=$(echo "REBUILD_${library}" | sed "s/\-/\_/g")
    DEPENDENCY_REBUILT_FLAG=$(echo "DEPENDENCY_REBUILT_${library}" | sed "s/\-/\_/g")

    if [[ $run -eq 1 ]] && [[ "${!BUILD_COMPLETED_FLAG}" != "1" ]]; then
      LIBRARY_IS_INSTALLED=$(library_is_installed "${LIB_INSTALL_BASE}" "${library}")

      echo -e "INFO: Flags detected for ${library}: already installed=${LIBRARY_IS_INSTALLED}, rebuild requested by user=${!REBUILD_FLAG}, will be rebuilt due to dependency update=${!DEPENDENCY_REBUILT_FLAG}\n" 1>>"${BASEDIR}"/build.log 2>&1

      # CHECK IF BUILD IS NECESSARY OR NOT
      if [[ ${LIBRARY_IS_INSTALLED} -ne 1 ]] || [[ ${!REBUILD_FLAG} -eq 1 ]] || [[ ${!DEPENDENCY_REBUILT_FLAG} -eq 1 ]]; then

        echo -n "${library}: "

        "${BASEDIR}"/scripts/run-apple.sh "${library}" 1>>"${BASEDIR}"/build.log 2>&1

        # SET SOME FLAGS AFTER THE BUILD
        if [ $? -eq 0 ]; then
          ((completed += 1))
          declare "$BUILD_COMPLETED_FLAG=1"
          check_if_dependency_rebuilt "${library}"
          echo "ok"
        else
          echo -e "failed\n\nSee build.log for details\n"
          exit 1
        fi
      else
        ((completed += 1))
        declare "$BUILD_COMPLETED_FLAG=1"
        echo "${library}: already built"
      fi
    else
      echo -e "INFO: Skipping $library, dependencies built=$run, already built=${!BUILD_COMPLETED_FLAG}\n" 1>>"${BASEDIR}"/build.log 2>&1
    fi
  done
done

# SKIP TO SPEED UP THE BUILD
if [[ ${SKIP_ffmpeg} -ne 1 ]]; then

  # PREPARE PATHS & DEFINE ${INSTALL_PKG_CONFIG_DIR}
  LIB_NAME="ffmpeg"
  set_toolchain_paths "${LIB_NAME}"

  # SET BUILD FLAGS
  HOST=$(get_host)
  export CFLAGS=$(get_cflags "${LIB_NAME}")
  export CXXFLAGS=$(get_cxxflags "${LIB_NAME}")
  export LDFLAGS=$(get_ldflags "${LIB_NAME}")
  export PKG_CONFIG_LIBDIR="${INSTALL_PKG_CONFIG_DIR}"

  cd "${BASEDIR}"/src/"${LIB_NAME}" 1>>"${BASEDIR}"/build.log 2>&1 || exit 1

  LIB_INSTALL_PREFIX="${LIB_INSTALL_BASE}"/"${LIB_NAME}"

  # BUILD FFMPEG
  source "${BASEDIR}"/scripts/apple/ffmpeg.sh "$@"

  if [[ $? -ne 0 ]]; then
    exit 1
  fi
else
  echo -e "\nffmpeg: skipped"
fi

# SKIP TO SPEED UP THE BUILD
if [[ ${SKIP_ffmpeg_kit} -ne 1 ]]; then

  # BUILD FFMPEG KIT
  . "${BASEDIR}"/scripts/apple/ffmpeg-kit.sh "$@" || exit 1
else
  echo -e "\nffmpeg-kit: skipped"
fi

echo -e "\nINFO: Completed build for ${ARCH} at $(date)\n" 1>>"${BASEDIR}"/build.log 2>&1
