version: '3'

tasks:

  default: task -l

  import-tools:
    desc: import tool documentation
    silent: true
    cmds:
      - test -d "openserverless-cli" && git pull || git clone https://github.com/apache/openserverless-cli
      - |
        find openserverless-cli/tools -name "*.md" | while read file
        do 
          name=$(basename $file .md)
          echo "Working on tool ${name} in file ${file}"
          mkdir -p content/en/docs/reference/tools/$name
          echo  -e "---\ntitle: $name\n---\n" > content/en/docs/reference/tools/$name/_index.md
          sed '/^# /d' "$file" >> content/en/docs/reference/tools/$name/_index.md
          # cat $file >> content/en/docs/reference/tools/$name/_index.md
        done
      - cp tools.md content/en/docs/reference/tools/_index.md
      - rm -rf openserverless-cli

  import-task:
    desc: import task documentation
    silent: true
    cmds:
      - test -d "openserverless-task" && git pull || git clone https://github.com/apache/openserverless-task
      - |
        find openserverless-task -name "docopts.md" | while read file
        do 
          tgt=$(echo $file | sed -e 's!openserverless-task/!!' -e 's/docopts/_index/')
          echo "> Converting ${file} to ${tgt}"
          mkdir -p "content/en/docs/reference/tasks/$(dirname $tgt)"
          name="$(basename $(dirname "Tasks"/$tgt))"
          description=$(awk '/^# /{flag=1; next} /^## /{flag=0} flag && NF' "$file")
          description=$(echo "$description" | tr -d '\n' | tr -d '\r' | xargs)
          remaining=$(awk '/^## /{flag=1} flag' "$file")
          echo  -e "---\ntitle: ${name^}\ndescription: ${description}\n---\n" >content/en/docs/reference/tasks/$tgt
          echo -e "$remaining" >>content/en/docs/reference/tasks/$tgt
          #cat $file >>content/en/docs/reference/tasks/$tgt
        done
      - rm -rf openserverless-task

  preview:
    silent: true
    desc: preview the websites
    cmds:
      - PATH="node_modules/.bin:$PATH" hugo server --baseURL "http://localhost:1313/" -O

  debug:
    silent: true
    desc: debug the websites
    cmds:
      - PATH="node_modules/.bin:$PATH" hugo server --ignoreCache  --debug --disableFastRender
