#!/bin/sh

kapow route add '/format/input' - <<-'EOF'
	kapow set /response/headers/Content-Type application/json
	pandoc --list-input-formats \
	| jq --raw-input --slurp 'split("\n") | .[0:-1]' \
	| kapow set /response/body
EOF

kapow route add '/format/output' - <<-'EOF'
	kapow set /response/headers/Content-Type application/json
	pandoc --list-output-formats \
	| jq --raw-input --slurp 'split("\n") | .[0:-1]' \
	| kapow set /response/body
EOF

kapow route add -X POST --entrypoint '/bin/zsh -c' '/convert' - <<-'EOF'
	kapow set /response/headers/Content-Type application/octet-stream
	kapow set /response/headers/Content-Disposition "attachment; filename=$(kapow get /request/files/inputfile/filename).$(kapow get /request/form/to)"
	pandoc --from=$(kapow get /request/form/from) \
	       --to=$(kapow get /request/form/to) \
	       --output=>(kapow set /response/body) \
	       =(kapow get /request/files/inputfile/content)
EOF

kapow route add / - <<-'EOF'
	kapow set /response/headers/Location /index.html
	kapow set /response/status 301
EOF

kapow route add /index.html - <<-'EOF'
	kapow set /response/headers/Content-Type text/html
	kapow set /response/body < index.html
EOF
