#!/bin/bash

#  Copyright 2015-2016 bill-auger <https://github.com/bill-auger/av-caster/issues>
#
#  This file is part of the AvCaster program.
#
#  AvCaster is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 3
#  as published by the Free Software Foundation.
#
#  AvCaster is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with AvCaster.  If not, see <http://www.gnu.org/licenses/>.


# enable logging (n=verbosity)
export GST_DEBUG=4
# comment this to log to screen
export GST_DEBUG_NO_COLOR=1 && export GST_DEBUG_FILE=debug.log # log to file
# enable graphviz
export DOT_DIR=/code/dot
export GST_DEBUG_DUMP_DOT_DIR=$DOT_DIR

# file output
VIDEOS_DIR=/home/bill/vids
OUTPUT_NAME="AvCaster"
OUTPUT_FILE=$OUTPUT_NAME.flv

# validate specified build configuration
if   [ "$CONFIG" == "Debug"   ] ; then BINARY=./build/av-caster-dbg ; TARGET="Debug" ;
else                                   BINARY=./build/av-caster     ; TARGET="Release" ;
fi

# build project
echo "Building $TARGET target"
CONFIG=$TARGET cd Builds/Makefile && make -j 2 && $BINARY $*

# convert dot graphs to images
if [ -d $DOT_DIR ]
then cd $DOT_DIR
     rm *.png 2> /dev/null
     for graph_dot_file in `ls *.dot 2> /dev/null`
     do echo "found dot graph $graph_dot_file"
       graph_png_file=${graph_dot_file%%.dot}.png
       dot -Tpng $graph_dot_file > $graph_png_file && rm $graph_dot_file
       [ -f $graph_png_file ] && echo "created graph image $graph_png_file"
     done
fi

# playback file output
if [ -d $VIDEOS_DIR -a -f $OUTPUT_FILE ]
then cd $VIDEOS_DIR
     filesize=`ls -s $OUTPUT_FILE`
     echo "output file exists $filesize"
     ((${filesize:0:1})) && mplayer $OUTPUT_FILE 2&> /dev/null
     rm $OUTPUT_NAME*
fi
