#!/bin/bash
export PATH=/home/owner/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export LANG=ja_JP.UTF-8

test $# -lt 1 && exit 1

DESTPATH="/media/owner/Backup/source"

minutes=${1}

COMMAND="er130-remocon.sh"
ARG="shot"


count=$(((minutes * 4) - 1))


timestring=`date '+%Y%m%d%H%M'`
mkdir "${DESTPATH}/JPG/${timestring}-${minutes}min"


	for ((i=${count};i>0;i--))
	do

surplus=$((${i} % 4))
second=$(date "+%S")

if test ${surplus} -eq 3; then
#	echo "毎00秒"
	if test ${second} -lt 15; then
		sleepsec=$(( 15 - ${second} ))
	else
		sleepsec=$(( 75 - ${second} ))
	fi
elif test ${surplus} -eq 2; then
#	echo "毎15秒"
	if test ${second} -lt 30; then
		sleepsec=$(( 30 - ${second} ))
	else
		sleepsec=$(( 90 - ${second} ))
	fi

elif test ${surplus} -eq 1; then
#	echo "毎30秒"
	if test ${second} -lt 45; then
		sleepsec=$(( 45 - ${second} ))
	else
		sleepsec=$(( 105 - ${second} ))
	fi
else
#	echo "毎45秒"
	sleepsec=$(( 60 - ${second} ))
fi
#echo "sleepsec ${sleepsec}"
sleep ${sleepsec}
#echo "shot `date`"
"${COMMAND}" "${ARG}"

	done
