#!/bin/sh
set -euC

##/// glslrun 1.0
#////
#//// Run GLSL code snippets on the CPU.
#////
###// usage:
#////   glslrun [options] [--] [<file>]
#////   glslrun -h|--help
#////   glslrun --version
#////
###// arguments:
####/   <file>
#////     File containing code to run. Specify - to read from stdin instead.
#////
###// options:
####/   -m, --main
#////     Generate an enclosing main() function.
#////
####/   -p, --print
#////     Print the generated code that is passed to the compiler.

## Messages
help()    { sed -n 's|^#[#/]*/ \?||p' "$0";              exit 0;    }
version() { help | awk '/^$/{++p;next}p==0';             exit 0;    }
usage()   { help | awk '/^$/{++p;next}p==2';             exit 0;    }
parse()   { printf '%s: error: %s\n'   "$0" "$1"; usage; exit 1;    } >&2
error()   { printf '%s: error: %s\n'   "$0" "$1";        exit 1;    } >&2
warning() { printf '%s: warning: %s\n' "$0" "$1";                   } >&2
opt()     { [ $# -gt 1 ] || parse "option '$1' value not provided"; }
arg()     { [ $# -gt 1 ] || parse "argument '$1' not provided";     }

## Parse special options
case "${1-}"
in
  '-h'|'--help') help; ;;
  '--version') version; ;;
esac

## Parse options
main=''
print=''
while [ $# -gt 0 ]
do
  case "$1"
  in
    '-m'|'--main') main='y'; ;;
    '-p'|'--print') print='y'; ;;
    '--') shift; break; ;;
    '-'?*) parse "unrecognized option '$1'"; ;;
    *) break; ;;
  esac
  shift
done

## Parse optional arguments
file="${1-}"; shift $(($#>0));

## Parse unrecognized arguments
[ $# -eq 0 ] || parse "unrecognized argument: '$1'"
