#!/bin/bash

# Simple script to snap photo with digital camera and post result.
# photoAutoBlog.sh uses gphoto2 to control a digital camera installed
#   with power in a preset location.  The script prompts the user for
#   information to be posted alongside the photo (questions about food
#   in this version of the script) and gives the user a preview of the
#   shot.  The script then saves the photo in its original format in
#   myOrigPhotosLoc, scales the photo down for two different web sizes 
#   with the "convert" command, and uploads all the images and text
#   description to the blog site.
#
# Run from the command line, the script first asks if the meal was
#   eaten out.  
#   Meal eaten out? <y/N> 
#   If you answer "y" or "Y", the script then asks for a restaurant 
#   name and generates a place holder image for the blog.  
#   If you answer "n" or "N" or hit enter, the script generates a 
#   pop-up asking for a short description of the food.  This is saved
#   in a simple text file.  The photo is then taken with the zoom
#   and flash settings specified in the "gphoto2" line below.
#   Lastly, the script uploads the photos to a directory created for
#   that purpose on the wordpress site using rsync.  The remote 
#   directory contains four files for each run of the script, for
#   example:
#   -rw-r--r-- 1 xuxa xuxa      53 2010-10-04 17:48 101004_174736.dat
#   -rw-r--r-- 1 xuxa xuxa 1066874 2010-10-04 17:48 101004_174736.jpg
#   -rw-r--r-- 1 xuxa xuxa  318464 2010-10-04 17:48 101004_174736_m.jpg
#   -rw-r--r-- 1 xuxa xuxa   13307 2010-10-04 17:48 101004_174736_s.jpg
#   The first set of six letters represents the date in yymmdd format
#   and the second set of six is the time the photo was acquired 
#   hhmmss.
#
#
# Copyright Kristofer Bergstrom 2010
#
# Released under the terms of the GNU GPLv3.
#
#
# Usage:
# at a terminal (command line), type
#   photoautoblog.sh
#



#
# Get the photo and name it with date_time
#

myOrigPhotoLoc="/data/imagesOrig/whatWereEating/"
mySmallPhotoLoc="/data/imagesOrig/whatWereEating/"
myMedPhotoLoc="/data/imagesOrig/whatWereEating/"
myPhotoTempName=$(date +%y%m%d_%H%M%S)

cd $myOrigPhotoLoc

# Check if the meal was eaten out
read -n 1 -p "Meal eaten out? <y/N> "
if [ "$REPLY" = "y" ]; then
echo "Restaurant name: "
read myRestName; 
myImageWidth=`identify -format %w /data/personal/lifeStats/meal_eaten_out_background.png`; \
convert -background '#0000' -gravity Center -size ${myImageWidth}x30 \
          caption:"$myRestName" \
          /data/personal/lifeStats/meal_eaten_out_background.png +swap -gravity south -composite $myPhotoTempName"_s.jpg";
echo "Restaurant link: (ENTER to skip): "
read myRestLink;
echo $myRestLink > $myPhotoTempName".dat"
echo "Returning to dir: " 
cd -
sleep .1
rsync -avt /data/imagesOrig/whatWereEating/ jikatabi\:/var/www/whatWereEating/
exit
fi

echo -e "Taking photo.  Please wait 15 seconds..."

gphoto2 --set-config zoom=8 --set-config flashmode=1 --capture-image-and-download --filename $myPhotoTempName 

myPhotoDesc=$(zenity --entry --text "Enter description:" --entry-text "" --title "Food description")
if [ $? != 0 ]
then
exit 0
fi

echo $myPhotoDesc > $myPhotoTempName".dat"

#
# Rotate, create thumbnail, and med-size version of the image
#

convert $myPhotoTempName -rotate 180 $myPhotoTempName".jpg"
sleep .5
feh -g 1600x1200 $myPhotoTempName".jpg"
convert $myPhotoTempName".jpg" -resize 5% $myPhotoTempName"_s.jpg"
convert $myPhotoTempName".jpg" -resize 50% $myPhotoTempName"_m.jpg"
sleep 1
rm $myPhotoTempName
sleep .1
rsync -avt /data/imagesOrig/whatWereEating/ jikatabi\:/var/www/whatWereEating/

echo "Returning to dir: " 
cd -



