~voidraven/s4

3bf2d1cdd90e6d9d8cbace90ce3de1b14488f9e4 — lotus 2 years ago 681fb4e
removed local keyword (not posix compliant), added more comments, version bump
2 files changed, 59 insertions(+), 51 deletions(-)

M .s4/watch.sh
M s4ctl.sh
M .s4/watch.sh => .s4/watch.sh +5 -5
@@ 39,19 39,19 @@ fun_set_false () {
# kick off
fun_record () {
    fun_set_true
    local VIDNAME=$1
    L_VIDNAME=$1
    # -y flag means always overwrite
    ffmpeg -y $FF_ARG_STD -framerate $FRAMERATE -video_size $REZ -i $WEBCAM "$CACHE_DIR/$VIDNAME.mkv" &
    ffmpeg -y $FF_ARG_STD -framerate $FRAMERATE -video_size $REZ -i $WEBCAM "$CACHE_DIR/$L_VIDNAME.mkv" &
}

# stop the camera
fun_stop () {
    # file to be copied (f.t.b.c) over to the main storage
    local FTBC=$1
    L_FTBC=$1
    fun_set_false
    kill $(pidof ffmpeg)
    if ! [ -z "$FTBC" ] ; then
	cp $CACHE_DIR/$FTBC.mkv $STORAGE/s4/cameras/$ID/$FTBC.mkv &
    if ! [ -z "$L_FTBC" ] ; then
	cp $CACHE_DIR/$L_FTBC.mkv $STORAGE/s4/cameras/$ID/$L_FTBC.mkv &
    fi
}


M s4ctl.sh => s4ctl.sh +54 -46
@@ 6,94 6,102 @@
#
# This file the main controller utility

VERSION="0.1"
VERSION="0.1.1"

GBL_ERROR_MSG=""

# create the s4 directory tree
fun_make_tree () {
    local CAMERAS=$1
    local STORAGE=$2
    local COUNT=0
    if ! [ -d $STORAGE/s4 ] ; then
	mkdir -p $STORAGE/s4/cameras
    L1_CAMERAS=$1
    L1_STORAGE=$2
    L1_COUNT=0
    if ! [ -d $L1_STORAGE/s4 ] ; then
	mkdir -p $L1_STORAGE/s4/cameras
    fi
    while [ $COUNT -lt $CAMERAS ] ; do
	mkdir $STORAGE/s4/cameras/$COUNT
	COUNT=$(($COUNT+1))
    while [ $L1_COUNT -lt $L1_CAMERAS ] ; do
	mkdir $L1_STORAGE/s4/cameras/$L1_COUNT
	L1_COUNT=$(($L1_COUNT+1))
    done
    for CAM in $(ls $STORAGE/s4/cameras) ; do
	echo "false" > $STORAGE/s4/cameras/$CAM/run.status
	echo "true" > $STORAGE/s4/cameras/$CAM/stop.status
    for CAM in $(ls $L1_STORAGE/s4/cameras) ; do
	echo "false" > $L1_STORAGE/s4/cameras/$CAM/run.status
	echo "true" > $L1_STORAGE/s4/cameras/$CAM/stop.status
	cp ~/.s4/watch.sh "./watch$CAM.sh"
    done
    unset L1_CAMERAS L1_STORAGE L1_COUNT
}

# make sure we received good user input
fun_check_input () {
    local CAMERAS=$1
    local REZ=$2
    if [ $CAMERAS -lt 0 ] ; then
	GBL_ERROR_MSG="Bad number of cameras given!"
    L2_CAMERAS=$1
    L2_REZ=$2
    if [ $L2_CAMERAS -lt 0 ] ; then
	GBL_ERROR_MSG="Bad number of cameras given! (cannot be negative)"
	unset L2_CAMERAS L2_REZ
	return 1
    elif  [ $CAMERAS -gt 99 ] ; then
	GBL_ERROR_MSG="Bad number of cameras given!"
    elif  [ $L2_CAMERAS -gt 99 ] ; then
	GBL_ERROR_MSG="Bad number of cameras given! (over 99 is too many)"
	unset L2_CAMERAS L2_REZ
	return 1
    fi
    unset L2_CAMERAS L2_REZ
    return 0
}

# edit each of the watch templates to use the provided settings
fun_edit_templates () {
    local CAMERAS=$1
    local REZ=$2
    L3_CAMERAS=$1
    L3_REZ=$2
    # this one liner escapes the '/' in the path, i.e. /mnt/nas becomes \/mnt\/nas
    local STORAGE=$(echo $3 | sed 's/\//\\\//g')
    local FRAMER8=$4
    local COUNT=0
    while [ $COUNT -lt $CAMERAS ] ; do
	printf ",s/XXX/$COUNT\n,s/YYY/$REZ\n,s/ZZZ/$STORAGE\n,s/WWW/$FRAMER8\nw\nq\n" \
	       | ed ./watch$COUNT.sh > /dev/null 2>&1
	COUNT=$(($COUNT+1))
    L3_STORAGE=$(echo $3 | sed 's/\//\\\//g')
    L3_FRAMER8=$4
    L3_COUNT=0
    while [ $L3_COUNT -lt $L3_CAMERAS ] ; do
	printf ",s/XXX/$L3_COUNT\n,s/YYY/$L3_REZ\n,s/ZZZ/$L3_STORAGE\n,s/WWW/$L3_FRAMER8\nw\nq\n" \
	       | ed ./watch$L3_COUNT.sh > /dev/null 2>&1
	L3_COUNT=$(($L3_COUNT+1))
    done
    unset L3_CAMERAS L3_REZ L3_STORAGE L3_FRAMER8 L3_COUNT
}

# creates watch files and config
fun_get_input () {
    local CAMERAS=""
    local REZ=""
    local STORAGE=""
    local FRAMER8=""
    L_CAMERAS=""
    L_REZ=""
    L_STORAGE=""
    L_FRAMER8=""
    printf "> How many cameras do you want to setup?: "
    read CAMERAS
    read L_CAMERAS
    # example valid resolutions: 640x480, 1280x720, 1920x1080
    printf "> What resolution are you recording at?: "
    read REZ
    read L_REZ
    printf "> What framerate do you want to record at? [30/60]: "
    read FRAMER8
    read L_FRAMER8
    printf "> Where do you want to store your video files?: "
    read STORAGE
    read L_STORAGE
    echo "Configuring storage..."
    echo "Generating watch files..."
    if fun_check_input $CAMERAS $REZ ; then
	local WRITE=""
    if fun_check_input $L_CAMERAS $L_REZ ; then
	L_WRITE=""
	if [ -s ~/.s4/config ] ; then
	    printf "> Config file already exists, did you want to overwrite it? [y/n]: "
	    read WRITE
	    read L_WRITE
	fi
	if [ "$WRITE" = "y" ] || [ "$WRITE" = "" ] ; then
	if [ "$L_WRITE" = "y" ] || [ "$L_WRITE" = "" ] ; then
	    echo "#!/bin/sh" > ~/.s4/config
	    echo "S4_CAMERAS=$CAMERAS" >> ~/.s4/config
	    echo "S4_REZ=$REZ" >> ~/.s4/config
	    echo "S4_CAMERAS=$L_CAMERAS" >> ~/.s4/config
	    echo "S4_REZ=$L_REZ" >> ~/.s4/config
	    echo "S4_STORAGE=$STORAGE" >> ~/.s4/config
	    fun_make_tree $CAMERAS $STORAGE
	    fun_edit_templates $CAMERAS $REZ $STORAGE $FRAMER8
	    fun_make_tree $L_CAMERAS $L_STORAGE
	    fun_edit_templates $L_CAMERAS $L_REZ $L_STORAGE $L_FRAMER8
	    echo "OK"
	    unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
	else
	    echo "Ok, exiting.."
	    unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
	    exit 1
	fi
    else
	unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
	fun_error $GBL_ERROR_MSG
    fi
}


@@ 121,15 129,15 @@ fun_check_config () {

# start/stop the camera(s)
fun_start_or_stop () {
    local ACTION=$1
    L_ACTION=$1
    fun_check_config
    . ~/.s4/config
    if [ $ACTION = "stop" ] ; then
    if [ $L_ACTION = "stop" ] ; then
	for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
	    echo "false" > $S4_STORAGE/s4/cameras/$CAM/run.status
	    echo "true" > $S4_STORAGE/s4/cameras/$CAM/stop.status
	done
    elif [ $ACTION = "start" ] ; then
    elif [ $L_ACTION = "start" ] ; then
	for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
	    echo "true" > $S4_STORAGE/s4/cameras/$CAM/run.status
	    echo "false" > $S4_STORAGE/s4/cameras/$CAM/stop.status