#!/bin/bash
# Copyright (C), 1998-2013, Tencent
# Author: jefftang@tencent.com
# Date: 2013-1-29

OLD_PWD=`pwd`
BLADE_DIR="$blade_dir"
BLADE_BOOST_DIR=~/.blade_boost
MOTD=/etc/motd

mkdir -p $BLADE_BOOST_DIR/bin

is_git_lfs_pulled() {
  local zipfile="$BLADE_DIR/blade.zip"
  local filesize
  if [[ "$(uname)" = "Darwin" ]]; then
    filesize=$(stat -f%z $zipfile)
  else
    filesize=$(stat --format=%s $zipfile)
  fi
  if [[ $filesize -gt 1024 ]]; then  # Unpulled LFS file is a small txt file
    return 0
  fi
  return 1
}

if ! is_git_lfs_pulled; then
  _error_exit "This repository need GIT LFS, see README.md"
fi

# On macOS, use system ninja/cmake instead of bundled Linux binaries. ninja is
# now always taken from PATH (the previously bundled binary was too old: < 1.10
# doesn't support multiple-output edges with the depslog).
if [[ "$(uname)" != "Darwin" ]]; then
  # Copy cmake (from thirdparty/blade/cmake-*.tar.gz).
  if [[ ! -L $BLADE_BOOST_DIR/bin/cmake ]]; then
    tar zxf $BLADE_DIR/cmake-3.31.6-linux-x86_64.tar.gz -C $BLADE_BOOST_DIR/
    ln -s $BLADE_BOOST_DIR/cmake-3.31.6-linux-x86_64/bin/cmake $BLADE_BOOST_DIR/bin/cmake
  fi

  # Copy nasm
  if [[ ! -f $BLADE_BOOST_DIR/bin/nasm ]]; then
    cp $BLADE_DIR/nasm $BLADE_BOOST_DIR/bin
    cp $BLADE_DIR/ndisasm $BLADE_BOOST_DIR/bin
  fi
fi

# Drop any stale ninja from a previous blade_init revision so we resolve ninja
# from the system PATH (apt install ninja-build / brew install ninja). Need
# >= 1.10 — older ninja errors out with "multiple outputs aren't (yet?)
# supported by depslog" on rules blade generates.
rm -f $BLADE_BOOST_DIR/bin/ninja
if ! command -v ninja >/dev/null 2>&1; then
  _error_exit "ninja not found; install via your package manager (e.g. \`apt install ninja-build\` or \`brew install ninja\`). Need >= 1.10."
fi
ninja_ver=$(ninja --version 2>/dev/null)
if [[ ! "$ninja_ver" =~ ^([0-9]+)\.([0-9]+) ]]; then
  _error_exit "Cannot parse ninja version: $ninja_ver"
fi
if (( ${BASH_REMATCH[1]} < 1 || (${BASH_REMATCH[1]} == 1 && ${BASH_REMATCH[2]} < 10) )); then
  _error_exit "ninja $ninja_ver is too old; need >= 1.10 for multiple-output edges with the depslog."
fi

PrepareDistBuild() {
  if [ ! -f $MOTD ]; then
    return
  fi
  tlinux=`cat $MOTD | grep 'tlinux 1.2 64bit'`
  if [ -z "$tlinux" ]; then
    return
  fi
  if [ -z "$TOOLCHAIN_DIR" ] && ! hash ccache 2>/dev/null; then
    if [ -z "$CC" ] && [ -z "$CXX" ]; then
      export GCCVERSION=`gcc -dumpversion`
      if [ "$GCCVERSION" == "4.9.4" ]; then
        export CC="python thirdparty/blade/dist/cxx.py gcc"
        export CXX="python thirdparty/blade/dist/cxx.py g++"
        return
      fi
    fi
  fi
}

# PrepareDistBuild

export PATH=$BLADE_BOOST_DIR/bin:$PATH
if [[ "$(uname)" != "Darwin" ]]; then
  export CMAKE_ROOT=$BLADE_BOOST_DIR/cmake-3.31.6-linux-x86_64/
fi

export GO111MODULE=on
export go_module_relpath=go/src/tencent.com
