#!/bin/sh

dir=/var/www/
cd $dir
rm index.html?action=snapshot*
wget http://localhost:8080/?action=snapshot -T 3 --tries=2

if [ -e  index.html?action=snapshot* ]; then
echo "image captured"

fil=$(date --date="+0 hours" +"%Y_%m_%d")
fil=$dir"archive/"$fil"/"                      # directory name
echo "directory " $fil

fil1=$(date --date="+0 hours" +"%Y.%m.%d_%T")  # file name
fil1=$fil$fil1".jpg"
echo "file name " $fil1

if [ -d $fil ]; then echo "storage directory already exists"
else mkdir $fil; cp webcam.sh.index $fil"index.html"
fi

cp index.html?action=snapshot $fil1
mv index.html?action=snapshot current.jpg

else
echo "could not get picture"
fi
