#!/bin/bash
HOST=192.168.0.45
USER=motion
PASS=motion

#
# First, wait for sunset
#
#   We get scheduled at 16:00 every day, which should be good for
#   both summer and winter
#
python sunrise.py sunset

#
# OK, sunset has fallen upon us, start the remote-cam runnin
#
#
sshpass -p$PASS ssh $USER@$HOST "motion -c .motion/motion.conf >motion.log 2>&1" &
PID=$!

#
# Camera job is now running in the background
#

#
# Wait for sunrise
#
python sunrise.py sunrise

#
# Sun has risen--rejoice
#
kill -1 $PID
sleep 1
kill $PID
sleep 2

sshpass -p$PASS ssh $USER@$HOST killall motion

sshpass -p$PASS scp $USER@$HOST:motion_data/*.mkv ./motion_data_$HOST
sshpass -p$PASS scp $USER@$HOST:motion_data/*.png ./motion_data_$HOST
sshpass -p$PASS ssh $USER@$HOST "rm -f motion_data/*.mkv"
sshpass -p$PASS ssh $USER@$HOST "rm -f motion_data/*.png"
