s4ctl.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/sh
  2. #
  3. # s4 - shell script sureveillance system
  4. # author: lotus
  5. # license: ISC
  6. #
  7. # This file the main controller utility
  8. VERSION="0.1.3"
  9. GBL_ERROR_MSG=""
  10. # NOTE: variable preceeded by "L_" are "local" variables
  11. # create the s4 directory tree
  12. # args: 2
  13. # 1: number of cameras we are working with
  14. # 2: video storage path ( /mnt/where/i/want/vids )
  15. fun_make_tree () {
  16. L1_CAMERAS=$1
  17. L1_STORAGE=$2
  18. L1_COUNT=0
  19. if ! [ -d $L1_STORAGE/s4 ] ; then
  20. mkdir -p $L1_STORAGE/s4/cameras
  21. fi
  22. while [ $L1_COUNT -lt $L1_CAMERAS ] ; do
  23. mkdir $L1_STORAGE/s4/cameras/$L1_COUNT
  24. L1_COUNT=$(($L1_COUNT+1))
  25. done
  26. for CAM in $(ls $L1_STORAGE/s4/cameras) ; do
  27. echo "false" > $L1_STORAGE/s4/cameras/$CAM/run.status
  28. echo "true" > $L1_STORAGE/s4/cameras/$CAM/stop.status
  29. cp ~/.s4/watch.sh "./watch$CAM.sh"
  30. done
  31. unset L1_CAMERAS L1_STORAGE L1_COUNT
  32. }
  33. # make sure we received good user input
  34. # args: 2
  35. # 1: number of cameras (1-99)
  36. # 2: video recording resolution (640x480, 1280x720, etc)
  37. fun_check_input () {
  38. L2_CAMERAS=$1
  39. L2_REZ=$2
  40. if [ $L2_CAMERAS -lt 0 ] ; then
  41. GBL_ERROR_MSG="Bad number of cameras given! (cannot be negative)"
  42. unset L2_CAMERAS L2_REZ
  43. return 1
  44. elif [ $L2_CAMERAS -gt 99 ] ; then
  45. GBL_ERROR_MSG="Bad number of cameras given! (over 99 is too many)"
  46. unset L2_CAMERAS L2_REZ
  47. return 1
  48. fi
  49. unset L2_CAMERAS L2_REZ
  50. return 0
  51. }
  52. # edit each of the watch templates to use the provided settings
  53. # args: 4
  54. # 1: number of cameras to set up
  55. # 2: video recording resolution (640x480, 1280x720, etc)
  56. # 3: root video storage location
  57. # 4: video recording framerate (60 for 60fps)
  58. fun_edit_templates () {
  59. L3_CAMERAS=$1
  60. L3_REZ=$2
  61. # this one liner escapes the '/' in the path, i.e. /mnt/nas becomes \/mnt\/nas
  62. L3_STORAGE=$(echo $3 | sed 's/\//\\\//g')
  63. L3_FRAMER8=$4
  64. L3_COUNT=0
  65. while [ $L3_COUNT -lt $L3_CAMERAS ] ; do
  66. printf ",s/XXX/$L3_COUNT\n,s/YYY/$L3_REZ\n,s/ZZZ/$L3_STORAGE\n,s/WWW/$L3_FRAMER8\nw\nq\n" \
  67. | ed ./watch$L3_COUNT.sh > /dev/null 2>&1
  68. L3_COUNT=$(($L3_COUNT+1))
  69. done
  70. unset L3_CAMERAS L3_REZ L3_STORAGE L3_FRAMER8 L3_COUNT
  71. }
  72. # creates watch files and config
  73. fun_get_input () {
  74. L_CAMERAS=""
  75. L_REZ=""
  76. L_STORAGE=""
  77. L_FRAMER8=""
  78. printf "> How many cameras do you want to setup?: "
  79. read L_CAMERAS
  80. # example valid resolutions: 640x480, 1280x720, 1920x1080
  81. printf "> What resolution are you recording at?: "
  82. read L_REZ
  83. printf "> What framerate do you want to record at? [30/60]: "
  84. read L_FRAMER8
  85. printf "> Where do you want to store your video files?: "
  86. read L_STORAGE
  87. echo "Configuring storage..."
  88. echo "Generating watch files..."
  89. if fun_check_input $L_CAMERAS $L_REZ ; then
  90. L_WRITE=""
  91. if [ -s ~/.s4/config ] ; then
  92. printf "> Config file already exists, did you want to overwrite it? [y/n]: "
  93. read L_WRITE
  94. fi
  95. if [ "$L_WRITE" = "y" ] || [ "$L_WRITE" = "" ] ; then
  96. echo "#!/bin/sh" > ~/.s4/config
  97. echo "S4_CAMERAS=$L_CAMERAS" >> ~/.s4/config
  98. echo "S4_REZ=$L_REZ" >> ~/.s4/config
  99. echo "S4_STORAGE=$L_STORAGE" >> ~/.s4/config
  100. # check if s4 folder exists in user provided path
  101. if ! [ -d "$L_STORAGE/s4" ] ; then
  102. fun_make_tree $L_CAMERAS $L_STORAGE
  103. fi
  104. fun_edit_templates $L_CAMERAS $L_REZ $L_STORAGE $L_FRAMER8
  105. echo "OK"
  106. unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
  107. else
  108. echo "Ok, exiting.."
  109. unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
  110. exit 1
  111. fi
  112. else
  113. unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
  114. fun_error $GBL_ERROR_MSG
  115. fi
  116. }
  117. fun_init () {
  118. # setup config file and make directory tree at storage location
  119. if ! [ -d "$HOME/.s4/" ] ; then
  120. fun_error "Missing config - You still need to run 'make deps' !"
  121. else
  122. # run user input loop
  123. fun_get_input
  124. fi
  125. }
  126. fun_check_config () {
  127. if ! [ -s "$HOME/.s4/config" ] ; then
  128. echo "No config file found at ~/.s4/config"
  129. echo "Make sure you have run 'make deps && make install' , then run:"
  130. echo "$ s4ctl -i"
  131. echo "in order to initialize you configuration"
  132. exit 1
  133. fi
  134. }
  135. # start/stop the camera(s)
  136. # args: 1
  137. # 1: action to perform; start or stop
  138. fun_start_or_stop () {
  139. L_ACTION=$1
  140. fun_check_config
  141. . ~/.s4/config
  142. if [ $L_ACTION = "stop" ] ; then
  143. for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
  144. echo "false" > $S4_STORAGE/s4/cameras/$CAM/run.status
  145. echo "true" > $S4_STORAGE/s4/cameras/$CAM/stop.status
  146. done
  147. elif [ $L_ACTION = "start" ] ; then
  148. for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
  149. echo "true" > $S4_STORAGE/s4/cameras/$CAM/run.status
  150. echo "false" > $S4_STORAGE/s4/cameras/$CAM/stop.status
  151. done
  152. fi
  153. }
  154. # list the camera(s) and their status(es)
  155. fun_list () {
  156. fun_check_config
  157. . ~/.s4/config
  158. for CAM in $(ls $S4_STORAGE/s4/cameras) ; do
  159. echo "Camera $CAM - Running Status: $(cat $S4_STORAGE/s4/cameras/$CAM/run.status)"
  160. done
  161. }
  162. # print out the version information
  163. fun_print_ver () {
  164. echo "version: $VERSION"
  165. }
  166. # print out the program info
  167. fun_print_info () {
  168. echo "s4 - the shell script surveillance system"
  169. fun_print_ver
  170. echo "author: https://git.zerohack.xyz/lotus"
  171. echo "license: ISC"
  172. }
  173. # print an error message
  174. # args: 1
  175. # 1: error message to print
  176. fun_error () {
  177. echo "Uh Oh, something borked!"
  178. echo $1
  179. fun_print_help
  180. exit 1
  181. }
  182. # prints out the help menu
  183. fun_print_help () {
  184. fun_print_info
  185. echo "--- usage --------------------------------------------------"
  186. echo "s4ctl -i # initialize a new configuration"
  187. echo "s4ctl -s # start the camera(s)"
  188. echo "s4ctl -p # stop the camera(s)"
  189. echo "s4ctl -l # list all the available cameras"
  190. echo "s4ctl -h # shows this help message"
  191. echo "s4ctl -v # show the version info"
  192. echo ""
  193. echo "s4 stores configuration/cache files in ~/.s4"
  194. }
  195. while getopts 'spilhv' OPT ; do
  196. case "$OPT" in
  197. s) fun_start_or_stop start;;
  198. p) fun_start_or_stop stop;;
  199. i) fun_init;;
  200. l) fun_list;;
  201. h) fun_print_help;;
  202. v) fun_print_ver && exit 0;;
  203. [?]) fun_error "Bad argument passed!";;
  204. esac
  205. done