#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
apiVersion: build.knative.dev/v1alpha1
kind: BuildTemplate
metadata:
  name: openwhisk-nodejs-runtime
spec:
  parameters:
  - name: TARGET_IMAGE_NAME
    description: name of the image to be tagged and pushed
  - name: TARGET_IMAGE_TAG
    description: tag the image before pushing
    default: "latest"
  - name: DOCKERFILE
    description: name of the dockerfile
  - name: OW_RUNTIME_DEBUG
    description: flag to indicate debug mode should be on/off
    default: "false"
  - name: OW_RUNTIME_PLATFORM
    description: flag to indicate the platform, one of ["openwhisk", "knative", ... ]
    default: "knative"
  - name: OW_ACTION_NAME
    description: name of the action
    default: ""
  - name: OW_ACTION_CODE
    description: JavaScript source code to be evaluated
    default: ""
  - name: OW_ACTION_MAIN
    description: name of the function in the "__OW_ACTION_CODE" to call as the action handler
    default: "main"
  - name: OW_ACTION_BINARY
    description: flag to indicate zip function, for zip actions, "__OW_ACTION_CODE" must be base64 encoded string
    default: "false"
  - name: OW_HTTP_METHODS
    description: list of HTTP methods, any combination of [GET, POST, PUT, and DELETE], default is [POST]
    default: "[POST]"
  - name: OW_ACTION_RAW
    description: flag to indicate raw HTTP handling, interpret and process an incoming HTTP body directly
    default: "false"
  - name: OW_PROJECT_URL
    description: Location to local/remote file storage or public/private GitHub repo from where JavaScript source code needs to be evaluated
    default: ""
  steps:
  - name: add-ow-env-to-dockerfile
    image: "gcr.io/kaniko-project/executor:debug"
    command:
    - /busybox/sh
    args:
    - -c
    - |
      if [ -z ${OW_PROJECT_URL} ]; then
        OW_ACTION_CODE="${OW_ACTION_CODE}"
      else
        if [ -f ${OW_PROJECT_URL} ]; then
          if [ ${OW_ACTION_BINARY} ]; then
            OW_ACTION_CODE=`base64 ${OW_PROJECT_URL}`
          else
            OW_ACTION_CODE=`cat ${OW_PROJECT_URL}`
          fi
        else
            TEMPDIR="knative-"$((1 + RANDOM % 100))
            TEMPFILE=`basename "${OW_PROJECT_URL}"`
            mkdir $TEMPDIR
            cd $TEMPDIR
            wget -O $TEMPFILE "${OW_PROJECT_URL}"
            OW_ACTION_CODE=`cat $TEMPFILE`
            cd ..
        fi
      fi
      cat <<EOF >> ${DOCKERFILE}
        ENV __OW_RUNTIME_DEBUG "${OW_RUNTIME_DEBUG}"
        ENV __OW_RUNTIME_PLATFORM "${OW_RUNTIME_PLATFORM}"
        ENV __OW_ACTION_NAME "${OW_ACTION_NAME}"
        ENV __OW_ACTION_CODE "$OW_ACTION_CODE"
        ENV __OW_ACTION_MAIN "${OW_ACTION_MAIN}"
        ENV __OW_ACTION_BINARY "${OW_ACTION_BINARY}"
        ENV __OW_HTTP_METHODS "${OW_HTTP_METHODS}"
        ENV __OW_ACTION_RAW "${OW_ACTION_RAW}"
        ENV __OW_PROJECT_URL "${OW_PROJECT_URL}"
      EOF
  - name: build-openwhisk-nodejs-runtime
    image: "gcr.io/kaniko-project/executor:latest"
    args: ["--destination=${TARGET_IMAGE_NAME}:${TARGET_IMAGE_TAG}", "--dockerfile=${DOCKERFILE}"]
