#!/bin/sh
set -eux

usage() {
  red "Missing argument $1.\n"
  cat <<EOF
Usage:
    record-my-screen.sh <OUTPUT_PATH> -[a|v]

      Arguments:
        OUTPUT_PATH        Path of the final WebM file
        -a                 Include audio
        -v                 Video only

Examples:
    Capture the screen without audio and save it in 'output.webm'
        record-my-screen.sh output.webm -v
EOF
}

OUTPUT_PATH="${1:-}"
[ -z "${OUTPUT_PATH}" ] && {
  usage 'OUTPUT_PATH'
  exit 2
}
shift

SCREEN_RESOLUTION="$(xrandr -q --current | grep '[*]' | awk '{print$1}')"
DEFAULT_FLAGS="
-f x11grab
-s $SCREEN_RESOLUTION
-i :0
"
while getopts 'av' flag; do
  case "$flag" in
    a)
      FLAGS="$DEFAULT_FLAGS"
      ;;
    v)
      FLAGS="$DEFAULT_FLAGS -f pulse -i default"
      ;;
		*)
			;;
	esac
done

if [ -z "${FLAGS:-}" ]; then
  usage '[a|b]'
  exit 2
fi

# shellcheck disable=2086
ffmpeg $FLAGS "$OUTPUT_PATH"

# https://github.com/georgmartius/vid.stab
