#!/bin/bash
##################
# Author: cn9479 #
##################

if [ ! $PATH_SOURCED ]; then
  source override.sh
  PATH_SOURCED=1
fi
APPSHELL_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"

GREEN='\e[1;32m'
REST='\e[0m'

Message(){
  echo -e "$GREEN$1$REST"
}

MKCD() {
  MKDIR $@
  CD $@
}

MKDIR(){
  mkdir -p $@
}

CD(){
  cd $@ || return
}

EXIST(){
  for it in "$@"; do
    test -e "$it" && echo "$it exist!" || return
  done
}

CMAKE() {
  cmake -Wno-dev -G Ninja -D CMAKE_BUILD_TYPE="$TUNE_BUILD_TYPE" -Wno-dev -D CMAKE_VERBOSE_MAKEFILE="$TUNE_OPTION_VERBOSE_MAKEFILE" -D DOXYGEN_GENERATE_RTF="$TUNE_OPTION_DOXYGEN_GENERATE_RTF" -D DOXYGEN_QUIET="$TUNE_OPTION_DOXYGEN_QUIET" -D CMAKE_CXX_STANDARD=11 -D CMAKE_INSTALL_RPATH_USE_LINK_PATH="$TUNE_OPTION_INSTALL_RPATH_USE_LINK_PATH" "$@"
}

BUILD() {
  cmake --build $@ --parallel
}

INSTALL() {
  cmake --install $@
}

TUNE_BuildAppShell() {
  SwitchRootDir
  DefinitionIdentify $@

  echo ""
  echo "build appshell..."

  MKCD "$BUILD_PREFIX/$TUNE_APPSHELL"

  local definitions

  if [ "$TUNE_ARCH" = "aarch64-none-linux-gnu" ]; then
    definitions=(
      -D CMAKE_INSTALL_PREFIX="$DIST_PREFIX"
      -D TUNE_DIST_PREFIX="$TUNE_DIST_PREFIX"
      -D ISP_VERSION="$TUNE_SDK_VERSION"
      -D ARCH=ARM64
      -D ISP_PLATFORM="$TUNE_PLATFORM"
      -D APPMODE=NATIVE
      -D QTLESS=$QTLESS
      -D WITH_DWE=1
      -D WITH_DRM=1
      -D WITH_FLEXA=$FLEXA_ENABLE
      -D SUBDEV_V4L2=1
      -D ISP_ENABLE_HDR="$TUNE_OPTION_HDR"
      -D ENABLE_IRQ=0
      -D SERVER_LESS=1
      -D SWTICH_PROJECT=$SWTICH_PROJECT
    )
  elif [ "$TUNE_ARCH" = "aarch64-oritek-linux" ]; then
    echo "aarch64-oritek-linux, build sdk with so"
    definitions=(
      -D CMAKE_INSTALL_PREFIX="$DIST_PREFIX"
      -D TUNE_DIST_PREFIX="$TUNE_DIST_PREFIX"
      -D ISP_VERSION="$TUNE_SDK_VERSION"
      -D ARCH=ARM64
      -D ISP_PLATFORM="$TUNE_PLATFORM"
      -D APPMODE=NATIVE
      -D STATIC_BUILD=0
      -D QTLESS=1
      -D SERVER_LESS=1
      -D SUBDEV_CHAR=1
      -D WITH_FLEXA=$FLEXA_ENABLE
      -D SUBDEV_V4L2=$SUBDEV_V4L2
      -D WITH_VVBENCH=1
      -D DUMP_IMAGE=$DUMP_IMAGE
      -D OFFLINE_TEST=$OFFLINE_TEST
      -D ISP_ENABLE_HDR="$TUNE_OPTION_HDR"
      -D JSONCPP_WITH_TESTS=OFF
      -D JSONCPP_WITH_POST_BUILD_UNITTEST=OFF
      -D BUILD_SHARED_LIBS=ON
      -D DUMMY_BUILD=0
      -D BASH_COMPILE=1
      -D SWTICH_PROJECT=$SWTICH_PROJECT
    )
  elif [ "$TUNE_OS" = "$TUNE_OS_LINUX" ]; then
    echo "$TUNE_OS sys, build sdk with so"
    definitions=(
      -D CMAKE_INSTALL_PREFIX="$DIST_PREFIX"
      -D TUNE_DIST_PREFIX="$TUNE_DIST_PREFIX"
      -D ISP_VERSION="$TUNE_SDK_VERSION"
      -D ARCH=X86
      -D ISP_PLATFORM="$TUNE_PLATFORM"
      -D APPMODE=NATIVE
      -D STATIC_BUILD=0
      -D QTLESS=$QTLESS
      -D SERVER_LESS=1
      -D SUBDEV_CHAR=1
      -D WITH_FLEXA=$FLEXA_ENABLE
      -D SUBDEV_V4L2=$SUBDEV_V4L2
      -D WITH_VVBENCH=1
      -D DUMP_IMAGE=$DUMP_IMAGE
      -D OFFLINE_TEST=$OFFLINE_TEST
      -D ISP_ENABLE_HDR="$TUNE_OPTION_HDR"
      -D JSONCPP_WITH_TESTS=OFF
      -D JSONCPP_WITH_POST_BUILD_UNITTEST=OFF
      -D BUILD_SHARED_LIBS=ON
      -D DUMMY_BUILD=0
      -D BASH_COMPILE=1
      -D SWTICH_PROJECT=$SWTICH_PROJECT
    )
  else
    echo "$TUNE_OS sys, build sdk with header file"
    definitions=(
      -D CMAKE_INSTALL_PREFIX="$DIST_PREFIX"
      -D TUNE_DIST_PREFIX="$TUNE_DIST_PREFIX"
      -D ISP_VERSION="$TUNE_SDK_VERSION"
      -D ARCH=X86
      -D ISP_PLATFORM="$TUNE_PLATFORM"
      -D APPMODE=NATIVE
      -D STATIC_BUILD=0
      -D QTLESS=$QTLESS
      -D SERVER_LESS=1
      -D SUBDEV_CHAR=1
      -D WITH_FLEXA=$FLEXA_ENABLE
      -D SUBDEV_V4L2=$SUBDEV_V4L2
      -D WITH_VVBENCH=1
      -D ISP_ENABLE_HDR="$TUNE_OPTION_HDR"
      -D JSONCPP_WITH_TESTS=OFF
      -D JSONCPP_WITH_POST_BUILD_UNITTEST=OFF
      -D BUILD_SHARED_LIBS=ON
      -D DUMMY_BUILD=1
      -D BASH_COMPILE=1
    )
  fi

  CMAKE "${definitions[@]}" "$PROJECT_ROOT/$TUNE_APPSHELL"
  BUILD . || return
  INSTALL .

  Message "The built binary dir is at $BUILD_PREFIX/appshell/generated/debug/bin"
  CD "$PROJECT_ROOT"
}

SwitchRootDir(){
  Message "APPSHELL_PATH is ${APPSHELL_PATH}"
  Message "TUNE_PATH_ROOT is ${TUNE_PATH_ROOT}"
  Message "TUNE_BUILD_TYPE is ${TUNE_BUILD_TYPE}"
  CD "$APPSHELL_PATH/.."
  PROJECT_ROOT=$(pwd)
  Message "PROJECT_ROOT is at $PROJECT_ROOT"
  BUILD_PREFIX="$PROJECT_ROOT/build/$TUNE_ARCH/$TUNE_BUILD_TYPE"
  Message "BUILD_PREFIX is at $BUILD_PREFIX"
  DIST_PREFIX="$PROJECT_ROOT/dist/$TUNE_ARCH/$TUNE_BUILD_TYPE"
  Message "DIST_PREFIX is at $DIST_PREFIX"
  TUNE_DIST_PREFIX="$TUNE_PATH_ROOT/dist/$TUNE_ARCH/$TUNE_BUILD_TYPE"
  Message "TUNE_DIST_PREFIX is at $TUNE_DIST_PREFIX"
}

ApplyArmToolChain(){
  echo "ARM_TOOLCHAIN_PATH = $ARM_TOOLCHAIN_PATH"
  sed -i 's#$HOME/My/opt/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu#$ARM_TOOLCHAIN_PATH#g' t_frameworks/t_install/interact/maintain/toolchain/aarch64-none-linux-gnu.sh || return
  . t_frameworks/t_install/interact/maintain/toolchain/aarch64-none-linux-gnu.sh
}

DefinitionIdentify(){
  FLEXA_ENABLE=0
  TUNE_PLATFORM=FPGA
  SUBDEV_V4L2=0
  QTLESS=0
  DUMP_IMAGE=0
  OFFLINE_TEST=0

  for arg in "$@"; do
    if echo "$arg" | grep -qi "qtless"; then
      QTLESS=1
      continue
    fi
    if echo "$arg" | grep -qi "cmodel"; then
      TUNE_PLATFORM=CMODEL
      continue
    fi
    if echo "$arg" | grep -qi "fpga"; then
      TUNE_PLATFORM=FPGA
      continue
    fi
    if echo "$arg" | grep -qi "flexa"; then
      TUNE_PLATFORM=FPGA
      FLEXA_ENABLE=1
      continue
    fi
    if echo "$arg" | grep -qi "v4l2"; then
      SUBDEV_V4L2=1
      continue
    fi
    if echo "$arg" | grep -qi "dump"; then
      DUMP_IMAGE=1
      continue
    fi
    if echo "$arg" | grep -qi "offline"; then
      OFFLINE_TEST=1
      continue
    fi
  done
}

DatabaseDefineControl(){
  local profile_cmake="$APPSHELL_PATH/../t_frameworks/t_database_c/cmake/profile/ISP8000_V2308.cmake"
  if [ -e "$profile_cmake" ]; then
    for it in "${@:1}"; do
      if ! grep -q "set($it ON)" $profile_cmake; then
        echo "set($it ON)" >> "$profile_cmake"
      fi
    done
  fi
}

DatabaseDefineControl T_DATABASE_AUTO_BLACK_LEVEL_SUBTRACTION T_DATABASE_AUTO_WIDE_DYNAMIC_RANGE_5_2 T_DATABASE_PHASE_DETECTION_AUTO_FOCUS || return

TuneBuild(){
  if [ "$1" = "t_common_c" ]; then
    function_name="TUNE_TuningCommonCInstall"
  elif [ "$1" = "t_json_c" ]; then
    function_name="TUNE_TuningJsonCInstall"
  else
    function_name="TUNE_TuningDatabaseCInstall"
  fi
  if [ "$2" = 0 ]; then
    if EXIST "$TUNE_BUILD_PREFIX"/$1/lib$1.so* && EXIST "$TUNE_INSTALL_PREFIX"/lib/lib$1.so*; then
      Message "$1 has been built, skip"
      echo "--"
    else
      $function_name || rm -rf $TUNE_BUILD_PREFIX/$1 || rm -rf $TUNE_BUILD_PREFIX/include/$1 || return
    fi
  else
    if EXIST "$TUNE_BUILD_PREFIX"/$1/lib$1.so* && EXIST "$TUNE_INSTALL_PREFIX"/lib/lib$1.so*; then
      Message "$1 has been built, remaking!"
      echo "--"
      rm -rf $TUNE_BUILD_PREFIX/$1 && rm -rf $TUNE_BUILD_PREFIX/include/$1
    fi
    $function_name || rm -rf $TUNE_BUILD_PREFIX/$1 || rm -rf $TUNE_BUILD_PREFIX/include/$1 || return
  fi
}

BuildAppShellWithDependencies(){
  source override.sh
  CD "t_frameworks/t_install"

  local sdk_shell="$APPSHELL_PATH/../t_frameworks/t_install/source/sdk/s_units.sh"
  until grep TUNE_CMakePatch "$sdk_shell" | grep "#" >/dev/null; do echo "disable TUNE_CMakePatch" && sed -i "/TUNE_CMakePatch/s/^/#/g" "$sdk_shell" || return; done

  . install.sh

  if [ "$TUNE_EDITION"!="units" ]; then
    TUNE_EDITION="units"
    TUNE_Edition
  fi

  if [ "$TUNE_SDK_VERSION_OLD" != "$TUNE_SDK_VERSION" ] || [ "$2" == "rebuild" ];then
    SWTICH_PROJECT=1
    TUNE_SDK_VERSION_OLD=$TUNE_SDK_VERSION
  else
    SWTICH_PROJECT=0
  fi
  TUNE_UNITS_ISP_VERSION="$TUNE_SDK_VERSION"

  if [ ! -d "$TUNE_PATH_TOOLS_TEMP/bin" ]; then
    mkdir -p "$TUNE_PATH_TOOLS_TEMP/bin"
  fi
  if [ ! -f "$TUNE_PATH_TOOLS_TEMP/bin/ninja" ]; then
    TUNE_ToolsNinja && Message "Ninja installed!" || return
  fi
  if [ ! -f "$TUNE_PATH_TOOLS_TEMP/bin/cmake" ]; then
    TUNE_ToolsCMake && Message "CMake installed!"|| return
  fi

  TUNE_Info
  TUNE_DependenciesMxmlInstall || return
  if [ -d "$TUNE_BUILD_PREFIX/units" ];then
    Message "Units has been built, skip"
    echo "--"
  else
    TUNE_UnitsInstall || ( rm -rf $TUNE_BUILD_PREFIX/units && return )
  fi
  export TUNE_EDITION_OPTION_SENSOR_IR=1
  TuneBuild t_common_c $SWTICH_PROJECT
  TuneBuild t_json_c $SWTICH_PROJECT
  TuneBuild t_database_c $SWTICH_PROJECT
  TUNE_BuildAppShell "${@:2}" || return
}

AppshellClean(){
  echo ""
  Message "clean appshell..."

  CD "${APPSHELL_PATH}/.."
  ROOT_DIR=$(pwd)

  rm -rf "$ROOT_DIR/dist/$TUNE_ARCH/$TUNE_BUILD_TYPE"
  rm -rf "$ROOT_DIR/build/$TUNE_ARCH/$TUNE_BUILD_TYPE/appshell"

  TUNE_UninstallAll
  CD "${APPSHELL_PATH}/.."
}

AppshellCleanAll(){
  echo ""
  Message "clean appshell..."

  CD "${APPSHELL_PATH}/.."
  ROOT_DIR=$(pwd)

  rm -rf "$ROOT_DIR/dist"
  rm -rf "$ROOT_DIR/build"

  Message "TUNE_UninstallAll..."
  TUNE_UninstallAll

  CD "$ROOT_DIR/t_frameworks"
  rm -rf "build"
  rm -rf "dist"
  rm -rf "dependencies/temp"
  rm -rf "tools/linux/temp"
  rm -rf "t_common_c/include/t_common_c_config.h"
  rm -rf "t_database_c/include/t_database_c_config.h"
  rm -rf "t_install/.config"
  rm -rf "t_json_c/include/t_json_c_config.h"
  
  CD "${APPSHELL_PATH}/.."
}


请解释一下这个shell脚本文件