#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)

. config/options "${1}"

if [ -z "${1}" ]; then
  die "usage: ${0} package_name [parent_pkg]"
fi

if [ -z "${PKG_NAME}" ]; then
  die "$(print_color CLR_ERROR "${1}: no package.mk file found")"
fi
PARENT_PKG="${2:-${PKG_NAME}}"

pkg_lock "${PKG_NAME}" "unpack" "${PARENT_PKG}"

${SCRIPTS}/get "${PKG_NAME}"

if [ -n "${PKG_DEPENDS_UNPACK}" ]; then
  export _unpack_recursive_cnt=$((_unpack_recursive_cnt + 1))
  if [ ${_unpack_recursive_cnt} -gt 2 ]; then
    die "unpack recursive limit hit: ${PKG_DEPENDS_UNPACK}, ${PARENT_PKG}"
  fi

  for p in ${PKG_DEPENDS_UNPACK}; do
    ${SCRIPTS}/unpack "${p}" "${PARENT_PKG}"
  done

  unset _unpack_recursive_cnt
fi

STAMP="${PKG_BUILD}/.libreelec-unpack"

mkdir -p ${BUILD}/build

# Perform a wildcard match on the package to ensure old versions are cleaned too
PKG_DEEPHASH=
for i in ${BUILD}/build/${PKG_NAME}-*; do
  if [ -d ${i} -a -f "${i}/.libreelec-unpack" ]; then
    . "${i}/.libreelec-unpack"
    if [ "${STAMP_PKG_NAME}" = "${PKG_NAME}" ]; then
      [ -z "${PKG_DEEPHASH}" ] && PKG_DEEPHASH=$(calculate_stamp)
      if [ ! "${PKG_DEEPHASH}" = "${STAMP_PKG_DEEPHASH}" ]; then
        ${SCRIPTS}/clean "${PKG_NAME}"
      fi
    fi
  fi
done

if [ -d "${PKG_BUILD}" -a ! -f "${STAMP}" ]; then
  # stale pkg build dir
  ${SCRIPTS}/clean "${PKG_NAME}"
fi

if [ -f "${STAMP}" ]; then
  pkg_lock_status "UNLOCK" "${PKG_NAME}" "unpack" "already unpacked"
  exit 0
fi

pkg_lock_status "ACTIVE" "${PKG_NAME}" "unpack"

if [ -d "${SOURCES}/${PKG_NAME}" -o -d "${PKG_DIR}/sources" ] || pkg_call_exists_opt unpack; then
  pkg_call_finish
  build_msg "CLR_UNPACK" "UNPACK" "${PKG_NAME}" "indent"

  # unpack into a unique location as unpacking into a single ${BUILD} directory is not thread-safe
  PKG_UNPACK_DIR="${BUILD}/.unpack/${PKG_NAME}"
  rm -rf "${PKG_UNPACK_DIR}"
  mkdir -p "${PKG_UNPACK_DIR}"

  # Save PKG_BUILD and point at our private unpack directory so that any
  # modifications to the content of ${PKG_BUILD} will be "safe".
  PKG_BUILD_ORIG="${PKG_BUILD}"
  PKG_BUILD="${PKG_UNPACK_DIR}/${PKG_NAME}-${PKG_VERSION}"

  pkg_call_exists_opt pre_unpack && pkg_call

  if pkg_call_exists unpack; then
    pkg_call
  else
    if [ -n "${PKG_URL}" ]; then
      ${SCRIPTS}/extract "${PKG_NAME}" "${PKG_UNPACK_DIR}"
    fi
    pkg_call_finish
  fi

  if [ -z "${PKG_SOURCE_DIR}" -a -d "${PKG_UNPACK_DIR}/${PKG_NAME}-${PKG_VERSION}"* ]; then
    mv "${PKG_UNPACK_DIR}/${PKG_NAME}-${PKG_VERSION}"* "${PKG_UNPACK_DIR}/.intermediate"
  fi

  if [ ! -d "${PKG_UNPACK_DIR}/.intermediate" ]; then
    if [ -n "${PKG_SOURCE_DIR}" ]; then
      if [ -d "${PKG_UNPACK_DIR}"/${PKG_SOURCE_DIR} ]; then
        mv "${PKG_UNPACK_DIR}"/${PKG_SOURCE_DIR} "${PKG_UNPACK_DIR}/.intermediate"
      else
        # fallback
        mv "${BUILD}"/${PKG_SOURCE_DIR} "${PKG_UNPACK_DIR}/.intermediate"
      fi
    fi
  fi

  [ ! -d "${PKG_UNPACK_DIR}/.intermediate" ] && mkdir -p "${PKG_UNPACK_DIR}/.intermediate"

  if [ -d "${PKG_DIR}/sources" ]; then
    cp -PRf "${PKG_DIR}/sources/"* "${PKG_UNPACK_DIR}/.intermediate"
  fi

  # Add a tag to the unpacked folder before transferring into the shared build folder
  echo "INFO_PKG_NAME=\"${PKG_NAME}\"" >"${PKG_UNPACK_DIR}/.intermediate/.libreelec-package"

  # Restore original PKG_BUILD, and transfer the unpacked folder
  PKG_BUILD="${PKG_BUILD_ORIG}"
  rm -fr "${PKG_BUILD}"
  mv "${PKG_UNPACK_DIR}/.intermediate" "${PKG_BUILD}"

  # cleanup
  rm -rf "${PKG_UNPACK_DIR}"

  pkg_call_exists_opt post_unpack && pkg_call

  if [ "${PKG_SKIP_PATCHES}" != "yes" ]; then
    pkg_call_exists_opt pre_patch && pkg_call

    if [ "${TARGET_ARCH}" = "x86_64" ]; then
      PATCH_ARCH="x86"
    elif [ "${PKG_IS_KERNEL_PKG}" = "yes" ]; then
      PATCH_ARCH="${TARGET_KERNEL_PATCH_ARCH:-${TARGET_ARCH}}"
    else
      PATCH_ARCH="${TARGET_PATCH_ARCH:-${TARGET_ARCH}}"
    fi

    PATCH_DIRS_PKG=""
    PATCH_DIRS_PRJ=""
    if [ -n "${PKG_PATCH_DIRS}" ]; then
      for patch_dir in ${PKG_PATCH_DIRS}; do
        if [[ ${patch_dir} =~ ^/ ]]; then
          [ -f ${patch_dir} ] && PATCH_DIRS_PKG+=" ${patch_dir}"
          [ -d ${patch_dir} ] && PATCH_DIRS_PKG+=" ${patch_dir}/*.patch"
        else
          [ -d ${PKG_DIR}/patches/${patch_dir} ] && PATCH_DIRS_PKG+=" ${PKG_DIR}/patches/${patch_dir}/*.patch"
          [ -d ${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${patch_dir} ] && PATCH_DIRS_PRJ+=" ${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${patch_dir}/*.patch"
          [ -d ${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/patches/${PKG_NAME}/${patch_dir} ] && PATCH_DIRS_PRJ+=" ${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/patches/${PKG_NAME}/${patch_dir}/*.patch"
        fi
      done
    fi

    for i in ${PKG_DIR}/patches/*.patch \
      ${PKG_DIR}/patches/${PATCH_ARCH}/*.patch \
      ${PATCH_DIRS_PKG} \
      ${PKG_DIR}/patches/${PKG_VERSION}/*.patch \
      ${PKG_DIR}/patches/${PKG_VERSION}/${PATCH_ARCH}/*.patch \
      ${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/*.patch \
      ${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${PATCH_ARCH}/*.patch \
      ${PATCH_DIRS_PRJ} \
      ${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${PKG_VERSION}/*.patch \
      ${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/patches/${PKG_NAME}/*.patch; do

      thisdir="${i%/*}"

      if [ "${thisdir}" = "${PKG_DIR}/patches" ]; then
        PATCH_DESC="(common)"
      elif [ "${thisdir}" = "${PKG_DIR}/patches/${PATCH_ARCH}" ]; then
        PATCH_DESC="(common - ${PATCH_ARCH})"
      elif [ "${thisdir}" = "${PKG_DIR}/patches/${PKG_VERSION}" ]; then
        PATCH_DESC="(common - ${PKG_VERSION})"
      elif [ "${thisdir}" = "${PKG_DIR}/patches/${PKG_VERSION}/${PATCH_ARCH}" ]; then
        PATCH_DESC="(${PKG_VERSION} - ${PATCH_ARCH})"
      elif [ "${thisdir}" = "${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}" ]; then
        PATCH_DESC="(project)"
      elif [ "${thisdir}" = "${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${PATCH_ARCH}" ]; then
        PATCH_DESC="(project - ${PATCH_ARCH})"
      elif [ "${thisdir}" = "${PROJECT_DIR}/${PROJECT}/patches/${PKG_NAME}/${PKG_VERSION}" ]; then
        PATCH_DESC="(project - ${PKG_VERSION})"
      elif [ "${thisdir}" = "${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/patches/${PKG_NAME}" ]; then
        PATCH_DESC="(device)"
      else
        if [[ "${thisdir}" =~ ^${PKG_DIR}/.* ]]; then
          PATCH_DESC="(common - ${thisdir##*/})"
        elif [[ "${thisdir}" =~ ^${PROJECT_DIR}/.*/devices/.* ]]; then
          PATCH_DESC="(device - ${thisdir##*/})"
        elif [[ "${thisdir}" =~ ^${PROJECT_DIR}/.* ]]; then
          PATCH_DESC="(project - ${thisdir##*/})"
        else
          PATCH_DESC="(absolute - ${i})"
        fi
      fi

      if [ -f "${i}" ]; then
        build_msg "CLR_APPLY_PATCH" "APPLY PATCH $(print_color "CLR_PATCH_DESC" "${PATCH_DESC}")" "${i#${ROOT}/}"
        if grep -qE '^GIT binary patch$|^rename from|^rename to' ${i}; then
          git apply --directory="${PKG_BUILD}" -p1 --verbose --whitespace=nowarn --unsafe-paths <${i} >&${VERBOSE_OUT}
        else
          patch -d "${PKG_BUILD}" -p1 <${i} >&${VERBOSE_OUT}
        fi
      fi
    done

    pkg_call_exists_opt post_patch && pkg_call
  fi

  if [ "${PKG_NAME}" != "configtools" ]; then
    for config in $(find "${PKG_BUILD}" -name config.guess | sed 's/config.guess//'); do
      build_msg "CLR_FIXCONFIG" "FIXCONFIG" "${config}"

      [ -f "${config}/config.guess" -a -f ${TOOLCHAIN}/configtools/config.guess ] &&
        cp -f ${TOOLCHAIN}/configtools/config.guess ${config}
      [ -f "${config}/config.sub" -a -f ${TOOLCHAIN}/configtools/config.sub ] &&
        cp -f ${TOOLCHAIN}/configtools/config.sub ${config}
    done
  fi
fi
pkg_call_finish

if [ "${PKG_SECTION}" != "virtual" ]; then
  mkdir -p "${PKG_BUILD}"
  echo "INFO_PKG_NAME=\"${PKG_NAME}\"" >"${PKG_BUILD}/.libreelec-package"

  PKG_DEEPHASH=$(calculate_stamp)
  for i in PKG_NAME PKG_DEEPHASH; do
    echo "STAMP_${i}=\"${!i}\"" >>${STAMP}
  done
fi

pkg_lock_status "UNLOCK" "${PKG_NAME}" "unpack" "unpacked"
