VERSION := `git rev-parse --short HEAD`

bindgen profile name:
    mkdir -p ./dist/{{name}}

    wasm-bindgen \
        --no-typescript \
        --target web \
        --out-dir ./dist/{{name}} \
        --out-name "bin" \
       ${CARGO_TARGET_DIR}/wasm32-unknown-unknown/{{profile}}/{{name}}.wasm

optimize NAME:
    # Size pass
    wasm-opt -Oz \
        -o dist/{{NAME}}/bin_bg-tmp.wasm \
        ./dist/{{NAME}}/bin_bg.wasm

    # Replace old bin with new (compressed) bin
    mv ./dist/{{NAME}}/bin_bg-tmp.wasm ./dist/{{NAME}}/bin_bg.wasm

build-example-dev EXAMPLE:
    cargo build --example {{EXAMPLE}} --profile wasm-dev --target wasm32-unknown-unknown

build-example EXAMPLE:
    cargo build --example {{EXAMPLE}} --profile release --target wasm32-unknown-unknown

build-bin GAME:
    # wasm binary
    cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown --features hide_debug

example-dev NAME: (build-example-dev NAME) (bindgen "wasm-dev" "examples"/NAME)
    cp web/example.html ./dist/examples/{{NAME}}/index.html

example NAME: (build-example NAME) (bindgen "release" "examples"/NAME)
    cp web/example.html ./dist/examples/{{NAME}}/index.html

# Build the web version
web GAME: (build-bin GAME) (bindgen "wasm-release" GAME) (optimize GAME)
    # base directory
    mkdir -p dist/{{GAME}}/assets/{{GAME}}

    # index.html
    cp ./web/{{GAME}}.html ./dist/{{GAME}}/index.html

    # assest
    rm -rf dist/{{GAME}}/assets/{{GAME}}
    cp -r assets/{{GAME}} dist/{{GAME}}/assets/{{GAME}}

# Helpful shorthand to serve files
serve GAME: (web GAME)
    simple-http-server dist/{{GAME}}

itchio-dry GAME: (web GAME)
    butler push --dry-run dist/{{GAME}} popgame/{{GAME}}:html5 --userversion={{VERSION}}

itchio GAME: (web GAME)
    butler push dist/{{GAME}} popgame/{{GAME}}:html5 --userversion={{VERSION}}

clean:
    rm -rf dist/*
