~voidraven/s4

5c00f37d8928d021150d95d3adc666f8384fe1b6 — lotus 1 year, 7 months ago cfa55ba
addressed more shellcheck issues
1 files changed, 54 insertions(+), 31 deletions(-)

M s4ctl.sh
M s4ctl.sh => s4ctl.sh +54 -31
@@ 16,24 16,25 @@ VERSION="0.1.3"
# 1: number of cameras we are working with
# 2: video storage path ( /mnt/where/i/want/vids )
fun_make_tree () {
    L1_CAMERAS=$1
    L1_STORAGE=$2
    L1_CAMERAS="$1"
    L1_STORAGE="$2"
    L1_COUNT=0
    if ! [ -d $L1_STORAGE/s4 ] ; then
	mkdir -p $L1_STORAGE/s4/cameras
    if ! [ -d "$L1_STORAGE"/s4 ] ; then
	mkdir -p "$L1_STORAGE"/s4/cameras
    fi
    while [ $L1_COUNT -lt $L1_CAMERAS ] ; do
	mkdir $L1_STORAGE/s4/cameras/$L1_COUNT
	L1_COUNT=$(($L1_COUNT+1))
	L1_COUNT=$((L1_COUNT+1))
    done
    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"
    for CAM in "$L1_STORAGE"/s4/cameras/* ; do
	echo "false" > "$CAM"/run.status
	echo "true" > "$CAM"/stop.status
	cp ~/.s4/watch.sh "./watch$(basename $CAM).sh"
    done
    unset L1_CAMERAS L1_STORAGE L1_COUNT
}


# make sure we received good user input
# args: 2
# 1: number of cameras (1-99)


@@ 42,7 43,7 @@ fun_check_input () {
    L2_CAMERAS=$1
    L2_REZ="$2"
    L_REZOK="FALSE"
    if [ $L2_CAMERAS -lt 1 -o $L2_CAMERAS -gt 99 ] ; then
    if [ "$L2_CAMERAS" -lt 1 ] || [ "$L2_CAMERAS" -gt 99 ] ; then
	unset L2_CAMERAS L2_REZ
	fun_error "Bad number of cameras given! (0 < cameras < 100)"
    elif  [ "$L2_REZ" = "1920x1080" ] ; then


@@ 63,6 64,7 @@ fun_check_input () {
    return 0
}


# edit each of the watch templates to use the provided settings
# args: 4
# 1: number of cameras to set up


@@ 79,11 81,12 @@ fun_edit_templates () {
    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))
	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 () {
    L_CAMERAS=""


@@ 91,21 94,21 @@ fun_get_input () {
    L_STORAGE=""
    L_FRAMER8=""
    printf "> How many cameras do you want to setup?: "
    read L_CAMERAS
    read -r L_CAMERAS
    # example valid resolutions: 640x480, 1280x720, 1920x1080
    printf "> What resolution are you recording at?: "
    read L_REZ
    read -r L_REZ
    printf "> What framerate do you want to record at? [30/60]: "
    read L_FRAMER8
    read -r L_FRAMER8
    printf "> Where do you want to store your video files?: "
    read L_STORAGE
    read -r L_STORAGE
    echo "Configuring storage..."
    echo "Generating watch files..."
    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 L_WRITE
	    read -r L_WRITE
	fi
	if [ "$L_WRITE" = "y" ] || [ "$L_WRITE" = "" ] ; then
	    echo "#!/bin/sh" > ~/.s4/config


@@ 129,6 132,7 @@ fun_get_input () {
    fi
}


fun_init () {
    # setup config file and make directory tree at storage location
    if ! [ -d "$HOME/.s4/" ] ; then


@@ 139,12 143,13 @@ fun_init () {
    fi
}


fun_check_config () {
    if ! [ -s "$HOME/.s4/config" ] ; then
	echo "No config file found at ~/.s4/config"
	echo "Make sure you have run 'make deps && make install' , then run:"
	echo "$ s4ctl -i"
	echo "in order to initialize you configuration"
	echo "in order to initialize your configuration"
	exit 1
    fi
}


@@ 154,36 159,49 @@ fun_check_config () {
# args: 1
# 1: action to perform; start or stop
fun_start_or_stop () {
    L_ACTION=$1
    L_ACTION="$1"
    fun_check_config
    # The preceding line makes sure that the config exists, ok to ignore SC1090
    # shellcheck source=/dev/null
    . ~/.s4/config
    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 [ $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
	done
    L_RUN_STAT="true" # defaults to start action
    L_STOP_STAT="false"
    if [ "$L_ACTION" = "stop" ] ; then
	L_RUN_STAT="false"
	L_STOP_STAT="true"
    fi
    # S4_STORAGE is brought in by sourcing the config file
    # will error earlier if not present, we can safely ignore the following SC2153 warning
    # shellcheck disable=SC2153
    for CAM in "$S4_STORAGE"/s4/cameras/* ; do
	echo "$L_RUN_STAT" > "$CAM"/run.status
	echo "$L_STOP_STAT" > "$CAM"/stop.status
    done
    unset L_RUN_STAT L_STOP_STAT
}


# list the camera(s) and their status(es)
fun_list () {
    fun_check_config
    # The preceding line makes sure that the config exists, ok to ignore SC1090
    # shellcheck source=/dev/null
    . ~/.s4/config
    for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
	echo "Camera $CAM - Running Status: $(cat $S4_STORAGE/s4/cameras/$CAM/run.status)"
    # S4_STORAGE is brought in by sourcing the config file
    # will error earlier if not present, we can safely ignore the following SC2153 warning
    # shellcheck disable=SC2153
    for CAM in "$S4_STORAGE"/s4/cameras/* ; do
	echo "Camera $(basename $CAM) - Running Status: $(cat "$CAM"/run.status)"
    done
}


# print out the version information
fun_print_ver () {
    echo "version: $VERSION"
}


# print out the program info
fun_print_info () {
    echo "s4 - the shell script surveillance system"


@@ 192,16 210,18 @@ fun_print_info () {
    echo "license: ISC"
}


# print an error message
# args: 1
# 1: error message to print
fun_error () {
    echo "Uh Oh, something borked!"
    echo $1
    echo "$1"
    fun_print_help
    exit 1
}


# prints out the help menu
fun_print_help () {
    fun_print_info


@@ 216,6 236,7 @@ fun_print_help () {
    echo "s4 stores configuration/cache files in ~/.s4"
}


while getopts 'spilhv' OPT ; do
	case "$OPT" in
	s)	fun_start_or_stop start;;


@@ 227,3 248,5 @@ while getopts 'spilhv' OPT ; do
	[?])	fun_error "Bad argument passed!";;
	esac
done

exit 0