remove some debug output
[photo-tags.git] / create_thumbnails.sh
1 #!/bin/bash
2
3 # init some variables
4 removemeta=0
5
6 # parse ini file 
7 CONFIG_FILE="config.ini"
8
9 # copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
10 eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
11     -e 's/;.*$//' \
12     -e 's/\[.*\]//' \
13     -e 's/[[:space:]]*$//' \
14     -e 's/^[[:space:]]*//' \
15     -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
16    < $CONFIG_FILE `
17
18 LOCALDB=$fspotdb
19 dbprefix=${dbprefix//\//\\\/}
20
21 # parse command line
22 while getopts ":r" opt; do
23   case $opt in
24     r)
25       removemeta=1
26       ;;
27     \?)
28       echo "Invalid option: -$OPTARG" >&2
29       ;;
30   esac
31 done
32
33
34 #make sure we are in the correct directory
35
36 #make sure we can access the database
37 if [ ! -s $LOCALDB ] ; then
38     echo "Can't find database...exiting."
39     exit 1
40 fi
41
42 #create directories, iff they don't exist
43 if [ ! -d "Photos-small" ] ; then
44     echo "creating local directory to store photos of reduced size"
45     mkdir Photos-small
46 fi
47
48 if [ ! -d "Photos-tiny" ] ; then
49     echo "creating local directory to store thumbnails"
50     mkdir Photos-tiny
51 fi
52
53 done=0
54 offset=0
55 FILES=""
56
57 while [ $done -lt 200 ] ; do
58    echo "skipping $offset pics, getting 100 new pics to work on..."
59    # handle white space in filename correctly
60    FILES=`sqlite3 $LOCALDB "select replace(base_uri||'/'||filename,' ','%20') from photos limit $offset,100"`
61    
62    if [ "x$FILES" != "x" ] ; then
63      #found some files, process them
64      for file in $FILES; do
65          # handle white space in file names
66          file=${file/file:\/\/$dbprefix/}
67          file=${file//\%20/ }
68          dir=`dirname "$file"`
69          base=`basename "$file"`
70          
71          if [ ! -s "Photos-tiny/$dir/$base" ] ; then
72              mkdir -p Photos-tiny/$dir
73              nice -19 convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
74              if [ $removemeta=1 ]; then
75                  jhead -q -se -purejpg "Photos-tiny/$dir/$base"
76                  jhead -q -se -dt "Photos-tiny/$dir/$base"
77                  jhead -q -se -mkexif "Photos-tiny/$dir/$base"
78                  jhead -q -se -cl "This photo belongs to $admin and was taken from $webbase. If you want to use this photo, please contact him." "Photos-tiny/$dir/$base"
79              fi
80              done=$((done+1))
81          fi 
82          if [ ! -s "Photos-small/$dir/$base" ] ; then
83              mkdir -p Photos-small/$dir
84              nice -19 convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
85              if [ $removemeta=1 ]; then
86                  jhead -q -se -purejpg "Photos-small/$dir/$base"
87                  jhead -q -se -dt "Photos-small/$dir/$base"
88                  jhead -q -se -mkexif "Photos-small/$dir/$base"
89                  jhead -q -se -cl "This photo belongs to $admin and was taken from $webbase. If you want to use this photo, please contact him." "Photos-small/$dir/$base"
90              fi
91              done=$((done+1))
92          fi 
93
94          echo -n -e "$((done/2))       \r"
95      done
96    else
97        break;
98    fi
99
100    #get ready to get the next 100
101    offset=$((offset+100))
102 done