#!/usr/bin/env cached-nix-shell
#! nix-shell -i bash -p ffmpeg slop

# Select a region and record it.
#
# If given a gif, it will record an mp4 and convert it afterwards using the
# mov2gif script included in this repo (which is powered by gifsicle and
# ffmpeg).
#
# Usage: scrrec somefile.mp4
#        scrrec another.gif

is_script=
geom=
delay=1
count=3
fps=30
stopfile=/tmp/_stop
while getopts c:d:f:g:ps opt; do
  case $opt in
    c) count=$OPTARG ;;
    d) delay="$OPTARG" ;;
    f) fps=$OPTARG ;;
    g) geom="$OPTARG" ;;
    p) compton= ;;
    s) is_script=1 ;;
  esac
done
shift $((OPTIND-1))

##
select-region() {
  if [[ $geom ]]; then
    echo "${geom//[x+]/ }"
  else
    slop -nof "%w %h %x %y"
  fi
}

countdown() {
  if (( count > 0 )); then
    for ((i=count;i>0;i--)); do
      if [[ $is_script ]]; then
        notify-send -u critical -t 1000 "Recording in $i"
      else
        echo "Recording in $i"
      fi
      sleep 1
    done
  elif [[ $is_script ]]; then
    notify-send -u critical "Starting recording..."
  else
    echo "Started recording..."
  fi
}

recording-start() {
  local ffmpeg_opts="-y -f x11grab -show_region 1 -ss $delay -s ${4}x${5} -i :0.0+${2},${3} -framerate ${fps}"
  local dest=${1:-./rec.mp4}
  local mp4dest=
  [[ $dest == *.gif ]] && mp4dest="${dest%.*}.mp4"
  countdown
  ffmpeg $ffmpeg_opts "${mp4dest:-$dest}" &
  pid=$!
  echo $pid >$stopfile
  wait $pid
  rm -f $stopfile
  if [[ -f "$mp4dest" ]]; then
    mov2gif "$mp4dest" "$dest" && rm -fv "$mp4dest"
  fi
  echo -n "$dest" | xclip -selection clipboard -in
  notify-send "Screencast successful" "Saved to $dest\nAnd copied to your clipboard!"
}


# If a recording session is already active, stop that one.
if [[ -n "$is_script" && -f "$stopfile" ]]; then
  kill $(cat $stopfile)
  echo "Stopped previous recording"
  exit 0
fi

read -r W H X Y < <(select-region)
[[ -z "$W$H$X$Y" ]] && exit 1

# Compton with blur and/or transparencies cause unbearable flickering in
# recordings, so temporarily disable it.
compton=
if systemctl is-enabled --user compton >/dev/null; then
  systemctl --user stop compton
  compton=1
fi
cleanup() { [[ $compton ]] && systemctl --user start compton; }
trap cleanup EXIT

# Let 'er rip
recording-start "$1" "$X" "$Y" "$W" "$H"

# vim:set ft=sh shiftwidth=2: