$if: 'event.event in build.allowed'
then:
  taskId: ${taskcluster.taskId}
  provisionerId: ${taskcluster.docker.provisionerId}
  workerType: ${build.workerType}
  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) }
    image: ${build.docker_image}

    env:
      TENSORFLOW_BUILD_ARTIFACT: ${build.tensorflow}
      DEBIAN_FRONTEND: "noninteractive"

    command:
      - "/bin/bash"
      - "--login"
      - "-cxe"
      - $let:
          extraSystemSetup: { $eval: strip(str(build.system_setup)) }
          extraSystemConfig: { $eval: strip(str(build.system_config)) }
        in: >
          adduser --system --home ${system.homedir.linux} ${system.username} &&
          apt-get -qq update && apt-get -qq -y install ${deepspeech.packages_xenial.apt} pixz pkg-config coreutils sudo unzip wget zip && ${extraSystemSetup} &&
          cd ${system.homedir.linux}/ &&
          echo -e "#!/bin/bash\nset -xe\n env && id && (wget -O - $TENSORFLOW_BUILD_ARTIFACT | pixz -d | tar -C ${system.homedir.linux}/ -xf - ) && cd ~/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/ && mkdir -p ${system.homedir.linux}/.cache/node-gyp/ && wget -O - ${system.node_gyp_cache.url} | tar -C ${system.homedir.linux}/.cache/node-gyp/ -xzf - && mkdir -p ${system.homedir.linux}/pyenv-root/ && wget -O - ${system.pyenv.linux.url} | tar -C ${system.homedir.linux}/pyenv-root/ -xzf - && if [ ! -z "${build.gradle_cache.url}" ]; then wget -O - ${build.gradle_cache.url} | tar -C ${system.homedir.linux}/ -xzf - ; fi && if [ ! -z "${build.android_cache.url}" ]; then wget -O - ${build.android_cache.url} | tar -C ${system.homedir.linux}/ -xzf - ; fi;" > /tmp/clone.sh && chmod +x /tmp/clone.sh &&
          sudo -H -u ${system.username} /bin/bash /tmp/clone.sh && ${extraSystemConfig} &&
          sudo -H -u ${system.username} --preserve-env /bin/bash ${system.homedir.linux}/DeepSpeech/ds/${build.scripts.build} &&
          sudo -H -u ${system.username} /bin/bash ${system.homedir.linux}/DeepSpeech/ds/${build.scripts.package}

    artifacts:
      "public":
        type: "directory"
        path: "/tmp/artifacts/"
        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}
