#!/usr/bin/env bash
set -Eeuo pipefail

# Taken from:
# https://www.pcsuggest.com/linux-screen-recorder-ffmpeg/

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

      Arguments:
        OUTPUT_PATH        Path of the final MKV file

Examples:
    Capture the screen and save it in 'output.mkv'
        record-my-screen.sh output.mkv
EOF
}

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


SCREEN_RESOLUTION="$(xrandr -q --current | grep '[*]' | awk '{print$1}')"

ffmpeg                      \
  -loglevel error           \
                            \
  `# audio options`         \
  -f pulse                  \
  -ac 2                     \
  -i default                \
                            \
  `# video options`         \
  -f x11grab                \
  -s "${SCREEN_RESOLUTION}" \
  -i "${DISPLAY}"           \
                            \
  "${OUTPUT_PATH}"
