taskId: ${taskcluster.taskId}
provisionerId: ${taskcluster.docker.provisionerId}
workerType: ${build.workerType}
taskGroupId: ${taskcluster.taskGroupId}
schedulerId: ${taskcluster.schedulerId}
created: { $fromNow: '0 sec' }
deadline: { $fromNow: '1 day' }
expires: { $fromNow: '6 months' }
scopes:
  - "index:insert-task:project.deepspeech.*"

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

  features:
    taskclusterProxy: true

  env:
    DEBIAN_FRONTEND: "noninteractive"

  command:
    - "/bin/bash"
    - "--login"
    - "-cxe"
    - $let:
        extraSystemSetup: { $eval: strip(str(build.system_setup)) }
        extraSystemConfig: { $eval: strip(str(build.system_config)) }
        taskIndexExpire: { $fromNow: '6 months' }
      in: >
        (apt-get -qq -y remove --purge ubuntu-advantage-tools || true) &&
        apt-get -qq update && apt-get -qq -y install curl git sudo && ${extraSystemSetup};
        cache_artifact=`curl -sSIL -o /dev/null -w "%{http_code}" ${build.cache.artifact_url}` &&
        if [ "$cache_artifact" != "200" ]; then
          adduser --system --home ${system.homedir.linux} ${system.username} && cd ${system.homedir.linux}/ &&
          mkdir -p /tmp/artifacts/ && chmod 777 /tmp/artifacts &&
          echo -e "#!/bin/bash\nset -xe\n env && id && (git clone --quiet ${build.build_or_cache.repo} ~/${build.build_or_cache.dir}/ && cd ~/${build.build_or_cache.dir}/ && git checkout --quiet ${build.build_or_cache.sha})" > /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}/${build.build_or_cache.dir}/${build.scripts.setup} &&
          sudo -H -u ${system.username} --preserve-env /bin/bash ${system.homedir.linux}/${build.build_or_cache.dir}/${build.scripts.build} &&
          sudo -H -u ${system.username} /bin/bash ${system.homedir.linux}/${build.build_or_cache.dir}/${build.scripts.package} &&
          sudo -H -u ${system.username} --preserve-env /bin/bash ${system.homedir.linux}/${build.build_or_cache.dir}/taskcluster/tc-update-index.sh ${taskIndexExpire} taskcluster ${build.cache.artifact_namespace}
        fi;

  artifacts:
    "public":
      type: "directory"
      path: "/tmp/artifacts/"
      expires: { $fromNow: '6 months' }

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