#!/bin/sh

if [ ! -z $1 ]
then
    if [ $1 == 'cut' ] 
    then
       ffmpeg -i "$2" -ss $3 -t $4 -c:v copy -c:a copy "$5"
    fi
    
    if [ $1 == 'shrink' ]
    then
       ffmpeg -i "$2" -vcodec libx264 -crf 15 -filter:v fps=25 -s 1280x720 -c:a copy "$3"
    fi

    if [ $1 == 'record' ]
    then
       device_id=$(pactl list short sources | grep output | awk '{print $1}')
       if [ $2 == 'mic' ]
       then
           microphone_id=$(pactl list short sources | grep input | awk '{print $1}')
           ffmpeg -framerate 25 -f x11grab -i :0.0 -f pulse -ac 2 -i $device_id -f pulse -ac 2 -i $microphone_id  -filter_complex "[1:0][2:0]amix=inputs=2:duration=shortest" ~/Videos/$(date "+%F-%N").mkv &
       else
	   ffmpeg -framerate 25 -f x11grab -i :0.0 -f pulse -ac 2 -i $device_id ~/Videos/$(date "+%F-%N").mkv &
       fi
       echo "$!" > ~/.local/share/ffmpeg/recent-recording
    fi
    
    if [ $1 == 'stop' ]
    then
       kill $(cat ~/.local/share/ffmpeg/recent-recording)
    fi

    if [ $1 == 'cue' ]
    then
       shnsplit -f $2 $3
    fi

    if [ $1 == 'mpc' ]
    then
	if [ $2 == 'update' ]
	then
	    mpc clear && systemctl --user restart mpd.service && sleep 5 && mpc update && mpc add / && mpc play
        else
	    if [ $(mpc status %$2%) == 'off' ]; then mpc $2 on; else mpc $2 off; fi
	fi
    fi

	    
else
    echo "cut:"
    echo "ffmpeg -i \$2 -ss \$3 -t \$4 -c:v copy -c:a copy \$5"
    echo "shrink:"
    echo "ffmpeg -i \$2 -vcodec libx264 -crf 15 -filter:v fps=25 -s 1280x720 -c:a copy\$3"
    echo "record:"
    echo "ffmpeg -framerate 25 -f x11grab -i :0.0 -f pulse -ac 2 -i 57 ~/Videos/\$(date \"+%F-%N\").mkv &"
    echo "cue:"
    echo "shnsplit -f \$2 \$3"
fi

