#!	/bin/csh -f
#
#	motioncam (motion detection camera)
#		(c) 2021	1YEN Toru
#
#
#	2021/08/07	ver.1.00
#
set myver="1.00"
set mypath=$0
set mynam=$mypath:t
#
set pir_pin=11						# wiringPI physical#
set t_rpt=600						# polling report interval [s]
set stl_opt="-t 1"					# additional raspistill option
set vid_opt=""						# additional raspivid option
#		additional option sample:
#		-w <width_dot>
#		-h <height_dot>
#		-hf		(horizontal flip)
#		-vf		(vertical flip)
#		etc., see "raspistill --help" more details
#
set t_stl=5							# take still image, interval [s]
set t_vid=0							# take video image, length [s]
set t_stp=0							# stop motioncam after [s]
while ( "$1" == "-h" || "$1" == "-tstl" || "$1" == "-tvid" || "$1" == "-tstp" || "$1" == "-dbg" )
	if ( "$1" == "-h" ) then
		echo "$mynam ver.$myver (motion detection camera)"
		echo "usage: $mynam [option] FileName"
		echo "output: FileNameD.jpg or FileNameD.h264"
		echo '        FileNameD=`date +"$FileName"`'
		echo "        FileName: do not include extension"
		echo '        eg) FileName="test_%y%m%d_%H%M%S"'
		echo "            FileNameD=test_210102_123456.{jpg or h264}"
		echo "option:"
		echo "        -tstl <tstl>    take a still image, interval <tstl>[s]"
		echo "        -tvid <tvid>    take a video image, length <tvid>[s]"
		echo "        -tstp <tstp>    stop $mynam after <tstp>[s]"
		echo "                            <tstp>=0: not stop $mynam (default)"
		echo "        -h              this message"
		echo "wirings:"
		echo "        5V(phys#2) --> PIR.VCC"
		echo "        GND(phys#9) <-> PIR.GND"
		echo "        GPIO(phys#"$pir_pin") <-- PIR.OUT"
		exit 0
	else if ( "$1" == "-tstl" ) then
		set t_stl=$2
		set t_vid=0
		shift
	else if ( "$1" == "-tvid" ) then
		set t_vid=$2
		set t_stl=0
		shift
	else if ( "$1" == "-tstp" ) then
		set t_stp=$2
		shift
	else if ( "$1" == "-dbg" ) then
		gpio -1 mode $pir_pin input
		set pir_pre=0
		while (1)
			set pir_out=`gpio -1 read $pir_pin`
			if ( $pir_pre != $pir_out ) then
				echo ""
				set pir_pre=$pir_out
			endif
			echo -n $pir_out
			sleep 1
		end
	endif
	shift
end
if ( $t_stl <= 0 && $t_vid <= 0 ) then
	# ERROR:
	echo $mynam": ERROR: requires one of -tstl or -tvid option."
	echo ""
	exit 0
endif
set fnam_out=$1
if ( "$fnam_out" == "" ) then
	echo $mynam": ERROR: FileName must be specified."
	echo ""
	exit 255
endif
#
# wiringPi gpio setting
gpio -1 mode $pir_pin input
#
set cur_sec=`date +%s`
set sta_sec=$cur_sec
while ( $t_stp == 0 || $cur_sec - $sta_sec < $t_stp )
	# polling PIR
	set pol_sec=0
	set pir_out=`gpio -1 read $pir_pin`
	while ( $pir_out == 0 && ( $t_stp == 0 || $cur_sec - $sta_sec < $t_stp ))
		set cur_sec=`date +%s`
		if ( $pol_sec == 0 || $cur_sec - $pol_sec > $t_rpt ) then
			echo "INF: pir_out=$pir_out `date +%c`"
			set pol_sec=`date +%s`
		endif
		sleep 1
		# continue
		set pir_out=`gpio -1 read $pir_pin`
		set cur_sec=`date +%s`
	end
	if ( $pir_out == 0 )\
		break

	# motion detected
	set fnam_cur=`date +"$fnam_out"`
	if ( $t_stl > 0) then
		# take a still image
		echo "DET: $fnam_cur.jpg `date +%c`"
		raspistill $stl_opt -o $fnam_cur.jpg
		sleep $t_stl
	else
		# take a video image
		echo "DET: $fnam_cur.h264 `date +%c`"
		raspivid $vid_opt -t $t_vid"000" -o $fnam_cur.h264
	endif

	# continue
	set cur_sec=`date +%s`
end
#
echo "INF: $mynam finished `date +%c`"

