# ArchLinux:
# Maintainer: Adrian Perez <aperez@igalia.com>
# Contributor: rway <rway07@gmail.com>
# Contributor: wabi <aschrafl@jetnet.ch>
# Contributor: Alexander RÃ¸dseth <rodseth@gmail.com>
# Contributor: Andreas Schrafl <aschrafl@gmail.com>
# Contributor: piojo <aur@zwell.net>
# Contributor: hack.augusto <hack.augusto@gmail.com>
# MSYS2:
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

pkgname=depot-tools-git
pkgver=r4311.56664461
pkgrel=1
pkgdesc='Build tools for working with Chromium development, include gclient'
arch=('any')
url='https://dev.chromium.org/developers/how-tos/install-depot-tools'
source=("${pkgname}::git+https://chromium.googlesource.com/chromium/tools/depot_tools.git"
        'depot_tools.sh' 'repo_fix.sh')
license=('Custom')
depends=('python2' 'python2-colorama') # 'python2-pylint')
makedepends=('git' 'scons' 'setconf')
provides=('depot_tools' 'gclient')
conflicts=('gclient-svn' 'depot_tools-svn')
options=('!strip')
sha256sums=('SKIP'
            'a08333544a746ae7482e4f70954997a7f14ffd4c13f2a5a62be6994cdb8115eb'
            '31040b1e39cb87f78cc542b1bda6830f10df3579f4bc98343e6fc3dbaf40c5ff')
install="depot_tools.install"

scripts_to_fix=(
  apply_issue
  cit
  clang-format
  clang_format_merge_driver
  commit_queue
  depot-tools-auth
  download_from_google_storage
  fetch
  gclient
  git-runhooks
  gn
  repo
  roll-dep
  roll-dep-svn
  weekly
  wtf
)

pkgver () {
  cd "${pkgname}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare () {
  cd "${pkgname}"

  # This tools work with python2, but MSYS2 default is python3. Fix it.
  # pylint is in extra, ninja is an executable and it does not need any change.
  # gclient.py require a fix for work correctly with python2-colorama
  # Another way is make default python2, but I don't think is a good idea!
  # Fixing python scripts.
  msg "Patching script for python2 usage..."
  for script in *.py
  do
    sed -i -r -e 's/#!(.*)python.*/#!\1python2/' "${script}"
  done

  # Fixing gclient.py
  sed -i -r -e 's/#!(.*)python.*/#!\1python2/' \
            -e 's/from third_party import colorama/import colorama/' \
            -e 's/from third_party.colorama import Fore/from colorama import Fore/' \
               gclient.py
  sed -i -e 's/exec python/exec python2/' \
             gclient

  # Fixing repo
  sed -r -i -e 's/"exec" python/"exec" python2/' repo

  # Fixing scripts in root folder
  for script in "${scripts_to_fix[@]}"
  do
    sed -r -i -e 's/exec python\b/exec python2/' \
              -e 's/#!(.*)python.*/#!\1python2/' \
                 "${script}"
  done

  # Fixing scripts in other folders
  # Is it safe remove those folders?? I don't know, further tests required. For now fix it and include all tools.
  # sed -r -i -e 's/python/python2/' git-cl-upload-hook

  for script in $(find -type f | xargs grep -Pn 'env python\b' | grep -Po '(?<=\./)[^:]+' | sort -u)
  do
    sed -r -i -e 's/env python\b/env python2/' "${script}"
  done
}

package()
{
  # Creating directories
  install -d "${pkgdir}/opt"

  cp -r "${srcdir}/${pkgname}" "${pkgdir}/opt/"

  # Export PATH
  install -Dm755 "${srcdir}/depot_tools.sh" "${pkgdir}/etc/profile.d/depot_tools.sh"

  # Install repo_fix.sh script
  install -Dm 755 "${srcdir}/repo_fix.sh" "${pkgdir}/opt/${pkgname}"

  # Install License
  install -Dm644 "${pkgdir}/opt/${pkgname}/LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  rm -rf "${pkgdir}/opt/${pkgname}/.git"
}
