#!/bin/bash

# ritoru_buraza ma 19.9.2011 17.43.07 +0300
#
# Changes since su 24.7.2011 19.49.06 +0300
#	- remove $CAMERC despite error
#
# Changes since pe 23.4.2010 11.09.56 +0300
#	- quotes around $WEBCAMDIR
#
# Changes since to 22.4.2010 10.23.54 +0300
#	- export LC_LANG prior to calling date
#
# Changes since la 27.3.2010 21.26.55 +0200:
#	- remove tmp camErc
#
# la 27.3.2010 21.26.55 +0200: First release



source "${XDG_CONFIG_HOME:-$HOME/.config}/mariginari/ritoru_buraza" >/dev/null 2>&1 || exit 1
test "x$WEBCAMDIR" = "x" && exit 1
pushd "$WEBCAMDIR" >/dev/null 2>&1 || exit 1

PATH="$HOME/bin:$PATH"
test -x `which raberu >/dev/null 2>&1` || exit 1

SNAPSHOT=`mktemp`.jpg
rm -f "${SNAPSHOT%.jpg}"
CAMERC=`mktemp`
(cat <<EOFC
[ftp]
do = 0

[grab]
archive_shot_every = 0
device = /dev/video0
temp_file = $SNAPSHOT
quality = 99

# lag reduction, takes 5 shots, discards the first 4, thus clearing mmap
# buffers
lag_reduce = 5

width  = 320
height = 240
EOFC
) > $CAMERC
camE -s -c $CAMERC >/dev/null 2>&1 || { rm -f $CAMERC; exit 1 }
rm -f $CAMERC

test -r "$SNAPSHOT" || exit 1
if [ -x `which myujikku_pureiying >/dev/null 2>&1` ]
then
	NP="`myujikku_pureiying`"
fi
SNAPSHOTTIME=$(date -d "`stat -c %y "$SNAPSHOT" | cut -d. -f1`" | cut -d+ -f1 | sed 's/[ ]*$//')
raberu "$SNAPSHOT" "$NP" "$SNAPSHOTTIME"
FRESH=`date +%Y-%m-%d_%H%M%S`.jpg
convert "$SNAPSHOT" "$FRESH" || exit 1
rm -f "$SNAPSHOT"
echo "`pwd`/$FRESH"
popd >/dev/null 2>&1
