#!/usr/bin/env bash

: '
  * Record sessions with Asciinema for the Librecode project!
  '

. "$(dirname $0)/util/logging.bash"

read -d '' usage <<EOUSAGE
Usage: $0 [-h|-i]

OPTIONS
    -h  Display this help.
    -i  Record stdin. (BE CAREFUL WITH SECRETS!)
EOUSAGE

SPOOPY_STDIN=''

while getopts 'hi' opt; do
  case "${opt}" in
    h)
      excat "$usage"
      ;;
    i)
      SPOOPY_STDIN='--stdin'
      ;;
    *)
      dedcat "$usage"
      ;;
  esac
done

shift $((OPTIND-1))

. /etc/os-release

timestamp=$(mktimestamp)

case "${ID}" in
  arch)
    SUB_ID="${BUILD_ID}"
    ;;
  debian)
    SUB_ID="${VERSION_CODENAME}"
    ;;
  *)
    SUB_ID="unknown"
    ;;
esac

rec_prefix="$HOME/librecode/sessions/mine/$ID/$SUB_ID"
mkdir -p "$rec_prefix"

asciinema rec $SPOOPY_STDIN "$rec_prefix/$timestamp"
