first few files for my tag-gallery... hopefully more to come
[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          echo $file
66
67          # handle white space in file names
68          file=${file/file:\/\/$dbprefix/}
69          file=${file//\%20/ }
70          dir=`dirname "$file"`
71          base=`basename "$file"`
72          
73          if [ ! -s "Photos-tiny/$dir/$base" ] ; then
74              mkdir -p Photos-tiny/$dir
75              nice -19 convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
76              if [ $removemeta=1 ]; then
77                  jhead -q -se -purejpg "Photos-tiny/$dir/$base"
78                  jhead -q -se -dt "Photos-tiny/$dir/$base"
79                  jhead -q -se -mkexif "Photos-tiny/$dir/$base"
80                  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"
81              fi
82              done=$((done+1))
83          fi 
84          if [ ! -s "Photos-small/$dir/$base" ] ; then
85              mkdir -p Photos-small/$dir
86              nice -19 convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
87              if [ $removemeta=1 ]; then
88                  jhead -q -se -purejpg "Photos-small/$dir/$base"
89                  jhead -q -se -dt "Photos-small/$dir/$base"
90                  jhead -q -se -mkexif "Photos-small/$dir/$base"
91                  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"
92              fi
93              done=$((done+1))
94          fi 
95
96          echo -n -e "$((done/2))       \r"
97      done
98    else
99        break;
100    fi
101
102    #get ready to get the next 100
103    offset=$((offset+100))
104 done