fixed tag display and clicking on a tag button now removes the tag
[photo-tags.git] / create_thumbnails.sh
1 #!/bin/bash
2
3 NICE="nice -19"
4
5 # init some variables
6 removemeta=0
7
8 # parse ini file
9 CONFIG_FILE="config.ini"
10
11 # copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
12 eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
13     -e 's/;.*$//' \
14     -e 's/\[.*\]//' \
15     -e 's/[[:space:]]*$//' \
16     -e 's/^[[:space:]]*//' \
17     -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
18    < $CONFIG_FILE `
19
20 LOCALDB=$fspotdb
21 dbprefix=${dbprefix//\//\\\/}
22 limit=`$NICE sqlite3 $LOCALDB "select count(*) from photos"`
23
24
25 function usage()
26 {
27   echo "Usage: $0 [OPTIONS]"
28   echo " Options:"
29   echo "          -r          remove metadata from pictures"
30   echo "          -l NUMBER   only process NUMBER pictures"
31 }
32
33 # parse command line
34 while getopts ":rl:h" opt; do
35   case $opt in
36     r)
37       removemeta=1
38       ;;
39     l)
40       limit=$OPTARG
41       ;;
42     h)
43       usage
44       exit 0
45       ;;
46     \?)
47       echo "Invalid option: -$OPTARG" >&2
48       exit 1
49       ;;
50     :)
51       echo "Option -$OPTARG requires an argument." >&2
52       exit 1
53       ;;
54   esac
55 done
56
57
58 #make sure we are in the correct directory
59
60 #make sure we can access the database
61 if [ ! -s $LOCALDB ] ; then
62     echo "Can't find database...exiting."
63     exit 1
64 fi
65
66 #create directories, iff they don't exist
67 if [ ! -d "Photos-small" ] ; then
68     echo "creating local directory to store photos of reduced size"
69     mkdir Photos-small
70 fi
71
72 if [ ! -d "Photos-tiny" ] ; then
73     echo "creating local directory to store thumbnails"
74     mkdir Photos-tiny
75 fi
76
77 done=0
78 offset=0
79 FILES=""
80
81 while [ $done -lt $limit ] ; do
82    echo "skipping $offset pics, getting $limit new pics to work on..."
83    # handle white space in filename correctly
84    FILES=`$NICE sqlite3 $LOCALDB "select replace(base_uri||'/'||filename,' ','%20') from photos limit $offset,$limit"`
85
86    if [ "x$FILES" != "x" ] ; then
87      #found some files, process them
88      for file in $FILES; do
89          # handle white space in file names
90          file=${file/file:\/\/$dbprefix/}
91          file=${file//\%20/ }
92          dir=`dirname "$file"`
93          base=`basename "$file"`
94
95          if [ ! -s "Photos-tiny/$dir/$base" ] ; then
96              mkdir -p Photos-tiny/$dir
97              $NICE convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
98              if [ $removemeta=1 ]; then
99                  $NICE jhead -q -se -purejpg "Photos-tiny/$dir/$base"
100                  $NICE jhead -q -se -dt "Photos-tiny/$dir/$base"
101                  $NICE jhead -q -se -mkexif "Photos-tiny/$dir/$base"
102                  $NICE 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"
103              fi
104              done=$((done+1))
105          fi
106          if [ ! -s "Photos-small/$dir/$base" ] ; then
107              mkdir -p Photos-small/$dir
108              $NICE convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
109              if [ $removemeta=1 ]; then
110                  $NICE jhead -q -se -purejpg "Photos-small/$dir/$base"
111                  $NICE jhead -q -se -dt "Photos-small/$dir/$base"
112                  $NICE jhead -q -se -mkexif "Photos-small/$dir/$base"
113                  $NICE 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"
114              fi
115              done=$((done+1))
116          fi
117
118          echo -n -e "$((done/2))       \r"
119      done
120    else
121        break;
122    fi
123
124    #get ready to get the next photos
125    offset=$((offset+$limit))
126 done