#!/bin/sh

screen=0
microphone=0
system_audio=0

select _sel in \
    "Record screen" \
    "Record microphone" \
    "Record system audio" \
    "Start recording" \
    "Quit"
do
    case "$_sel" in
        "Record screen") screen=1; ;;
        "Record microphone") microphone=1; ;;
        "Record system audio") system_audio=1; ;;
        "Start recording") break; ;;
        "Quit") exit 0; ;;
    esac

[ screen -eq 1 ] && echo "Screen recording: enabled" \
                 || echo "Screen recodding: disabled"
[ microphone -eq 1 ] && echo "Microphone recording: enabled" \
                     || echo "Microphone recodding: disabled"

done

clear

set -x

AUDIOMONDEVICE=snd/0.mon
AUDIOPLAYDEVICE=snd/0.default
AUDIORECDEVICE=snd/0.rec

AUDIOMONPID=X
AUDIORECPID=X

trap rec_stop 0 1 2 3 6
rec_stop() {
  kill $AUDIORECPID $AUDIOMONPID
}

rec_mon() {
      aucat -f $AUDIOMONDEVICE -o $HOME/work/rec-mon.wav &
      AUDIOMONPID=$!
}

rec_mic() {
      aucat -o $HOME/work/rec-mic.wav &
      AUDIORECPID=$!
}

rec_screen() {
    echo 'Press q to stop.'
    ffmpeg  -y -loglevel warning -hide_banner \
            -fflags genpts \
            -flags low_delay \
            -thread_queue_size 256 \
            -framerate 30 \
            -f x11grab \
            -probesize 16M \
            -i :0.0 \
            -c:v libx264rgb \
            -crf 0 \
            -qp 0 -framerate 30 \
            -sws_flags neighbor \
            -preset ultrafast \
            -tune zerolatency \
            $HOME/work/rec-screen.mkv
}

rec_mon
rec_mic
rec_screen
rec_stop

shotcut --noupgrade --fullscreen --clear-recent $HOME/work/rec-screen.mkv $HOME/work/rec-*.wav
