taskId: ${taskcluster.taskId}
provisionerId: ${taskcluster.docker.provisionerId}
workerType: ${taskcluster.docker.workerTypeWin}
taskGroupId: ${taskcluster.taskGroupId}
schedulerId: ${taskcluster.schedulerId}
dependencies:
  $map: { $eval: build.dependencies }
  each(b):
    $eval: as_slugid(b)
created: { $fromNow: '0 sec' }
deadline: { $fromNow: '1 day' }
expires:
  $if: '(event.event == "push") || (event.event == "tag")'
  then: { $fromNow: '6 months' }
  else: { $fromNow: '7 days' }

extra:
  nc_asset_name: { $eval: build.nc_asset_name }

routes:
  $if: '(event.event == "push") || (event.event == "tag")'
  then:
    { $eval: build.routes }

payload:
  maxRunTime: { $eval: to_int(build.maxRunTime) }

  env:
    TC_MSYS_VERSION: 'MSYS_NT-6.3-9600'
    MSYS: 'winsymlinks:nativestrict'
    GIT_LFS_SKIP_SMUDGE: '1'
    EXAMPLES_CLONE_URL: "https://github.com/coqui-ai/STT-examples"
    EXAMPLES_CHECKOUT_TARGET: "main"

  command:
    - >-
      "C:\Program Files\7-zip\7z.exe" x -txz -so msys2-base-x86_64.tar.xz |
      "C:\Program Files\7-zip\7z.exe" x -o%USERPROFILE% -ttar -aoa -si
    - .\msys64\usr\bin\bash.exe --login -cx "export THIS_BASH_PID=$$; ps -ef | grep '[?]' | awk '{print $2}' | grep -v $THIS_BASH_PID | xargs -r kill; exit 0"
    - .\msys64\usr\bin\bash.exe --login -cx "pacman -Syu --noconfirm"
    - echo .\msys64\usr\bin\bash.exe --login -cxe "
      export LC_ALL=C &&
      export PATH=\"/c/builds/tc-workdir/msys64/usr/bin:/c/Python36:/c/Program Files/Git/bin:/c/Program Files/7-Zip/:/c/Program Files (x86)/Windows Kits/10/bin/x64/:/c/Program Files/dotnet/:$PATH\" &&
      export TASKCLUSTER_ARTIFACTS=\"$USERPROFILE/public\" &&
      export TASKCLUSTER_TASK_DIR=\"/c/builds/tc-workdir/\" &&
      (rm -fr $TASKCLUSTER_TASK_DIR/ ; mkdir $TASKCLUSTER_TASK_DIR) && cd $TASKCLUSTER_TASK_DIR &&
      env &&
      ln -s $USERPROFILE/msys64 $TASKCLUSTER_TASK_DIR/msys64 &&
      (7z x -txz -so $USERPROFILE/home.tar.xz | 7z x -aoa -ttar -si ) &&
      git clone --quiet $EXAMPLES_CLONE_URL $TASKCLUSTER_TASK_DIR/DeepSpeech/examples &&
      cd $TASKCLUSTER_TASK_DIR/DeepSpeech/examples && git checkout --quiet $EXAMPLES_CHECKOUT_TARGET &&
      cd $TASKCLUSTER_TASK_DIR/DeepSpeech/ds && git remote set-url origin ${event.head.repo.url} && git fetch origin && git checkout --quiet ${event.head.sha} &&
      git submodule --quiet sync tensorflow/ && git submodule --quiet update tensorflow/ &&
      (rm $TASKCLUSTER_TASK_DIR/DeepSpeech/ds/tensorflow/native_client || true) &&
      ln -s $TASKCLUSTER_TASK_DIR/DeepSpeech/ds/native_client/ $TASKCLUSTER_TASK_DIR/DeepSpeech/ds/tensorflow/native_client &&
      cd $TASKCLUSTER_TASK_DIR &&
      (mkdir pyenv-root/ && 7z x -so $USERPROFILE/pyenv.tar.gz | 7z x -opyenv-root/ -aoa -ttar -si ) &&
      pacman --noconfirm -S tar make &&
      pacman --noconfirm -S zip &&
      $TASKCLUSTER_TASK_DIR/DeepSpeech/ds/${build.scripts.build} &&
      $TASKCLUSTER_TASK_DIR/DeepSpeech/ds/${build.scripts.package} ;
      echo \"export TASKCLUSTER_TASK_EXIT_CODE=$?\" > $USERPROFILE/tc-exit.sh &&
      cd $TASKCLUSTER_TASK_DIR/../ && rm -fr tc-workdir/ && exit $TASKCLUSTER_TASK_EXIT_CODE" | cmd
      /k ""C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"" x64

    - .\msys64\usr\bin\bash.exe --login -cxe "source $USERPROFILE/tc-exit.sh &&
      exit $TASKCLUSTER_TASK_EXIT_CODE"

  mounts:
    - file: msys2-base-x86_64.tar.xz
      content:
        sha256: ${system.msys2.sha}
        url: ${system.msys2.url}
    - file: home.tar.xz
      content:
        url: ${build.tensorflow}
    - file: pyenv.tar.gz
      content:
        url: ${system.pyenv.win.url}
    - directory: AppData/Local/node-gyp/Cache
      format: tar.gz
      content:
        url: ${system.node_gyp_cache.url}

  artifacts:
    - type: "directory"
      path: "public/"
      expires:
        $if: '(event.event == "push") || (event.event == "tag")'
        then: { $fromNow: '6 months' }
        else: { $fromNow: '7 days' }

metadata:
  name: ${build.metadata.name}
  description: ${build.metadata.description}
  owner: ${event.head.user.email}
  source: ${event.head.repo.url}
