#!/usr/bin/env bash
# Fixture HTTP para o `go-live-check.sh`: responde de forma coerente em todas as rotas que o
# script sonda. `FAKE_BREAK=<regra>` quebra uma regra por vez para provar que o gate falha.
set -u

url=''
dump=0
body=1
write=''
method=GET
gzip=0
inm=''

while [ $# -gt 0 ]; do
    case "$1" in
        -D) dump=1; shift 2 ;;
        -o) body=0; shift 2 ;;
        -w) write="$2"; shift 2 ;;
        -I) method=HEAD; shift ;;
        -X) method="$2"; shift 2 ;;
        -H)
            case "$2" in
                Accept-Encoding:*) [[ "$2" == *gzip* ]] && gzip=1 ;;
                If-None-Match:*) inm="${2#*: }" ;;
            esac
            shift 2
            ;;
        --max-time|-m) shift 2 ;;
        -sS|-fsS|-fsSI|-sSI|-s|-f) shift ;;
        http://*|https://*) url="$1"; shift ;;
        *) shift ;;
    esac
done

rest="${url#*://}"
path="/${rest#*/}"

etag_base='"abc123"'
etag="$etag_base"
[ "$gzip" = 1 ] && etag='"abc123-gzip"'

status=200
if [ "$method" = "TRACE" ]; then
    status=405
    [ "${FAKE_BREAK:-}" = "trace" ] && status=200
fi

case "$path" in
    /.env|/composer.json|/artisan)
        status=404
        [ "${FAKE_BREAK:-}" = "forbidden" ] && status=200
        [ "${FAKE_BREAK:-}" = "deny" ] && status=403
        ;;
esac

if [ "$method" != "TRACE" ] && [ -n "$inm" ] && [ "$inm" = "$etag" ]; then
    status=304
    [ "${FAKE_BREAK:-}" = "304" ] && status=200
fi

case "$path" in
    /sitemap.xml) ctype='application/xml' ;;
    /robots.txt|/.well-known/security.txt) ctype='text/plain; charset=UTF-8' ;;
    /build/assets/*) ctype='text/javascript' ;;
    *) ctype='text/html; charset=UTF-8' ;;
esac

cache='public, max-age=300, s-maxage=300, stale-while-revalidate=86400'
case "$path" in
    /build/assets/*)
        cache='public, max-age=31536000, immutable'
        [ "${FAKE_BREAK:-}" = "immutable" ] && cache='public, max-age=300'
        ;;
    /sitemap.xml|/robots.txt|/.well-known/security.txt) cache='public, max-age=300' ;;
esac

if [ "$dump" = 1 ]; then
    printf 'HTTP/1.1 %s OK\r\n' "$status"
    printf 'Content-Type: %s\r\n' "$ctype"
    printf 'Content-Security-Policy: default-src '\''self'\''\r\n'
    printf 'X-Content-Type-Options: nosniff\r\n'
    printf 'Referrer-Policy: strict-origin-when-cross-origin\r\n'
    printf 'Cache-Control: %s\r\n' "$cache"
    printf 'ETag: %s\r\n' "$etag"
    [ "${FAKE_BREAK:-}" = "hsts" ] || printf 'Strict-Transport-Security: max-age=31536000\r\n'
    if [ "$gzip" = 1 ] && [ "${FAKE_BREAK:-}" != "compression" ] && [ "$status" = 200 ]; then
        printf 'Content-Encoding: gzip\r\n'
    fi
    if [ "${FAKE_BREAK:-}" = "cookie" ] && [ "$path" = "/" ]; then
        printf 'Set-Cookie: laravel_session=fixture\r\n'
    fi
    printf '\r\n'
fi

if [ "$body" = 1 ] && [ "$method" = "GET" ]; then
    case "$path" in
        /) printf '<!doctype html><html><head><script src="/build/assets/app-abc.js"></script></head><body>ok</body></html>' ;;
        /build/assets/app-abc.js) printf 'console.log(1)' ;;
    esac
fi

if [ -n "$write" ]; then
    printf '%s' "$write" | sed "s/%{http_code}/$status/g"
fi
