fix displaying of tags that include a whitespace (use + instead)
[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
23 # parse command line
24 while getopts ":r" opt; do
25   case $opt in
26     r)
27       removemeta=1
28       ;;
29     \?)
30       echo "Invalid option: -$OPTARG" >&2
31       ;;
32   esac
33 done
34
35
36 #make sure we are in the correct directory
37
38 #make sure we can access the database
39 if [ ! -s $LOCALDB ] ; then
40     echo "Can't find database...exiting."
41     exit 1
42 fi
43
44 #create directories, iff they don't exist
45 if [ ! -d "Photos-small" ] ; then
46     echo "creating local directory to store photos of reduced size"
47     mkdir Photos-small
48 fi
49
50 if [ ! -d "Photos-tiny" ] ; then
51     echo "creating local directory to store thumbnails"
52     mkdir Photos-tiny
53 fi
54
55 done=0
56 offset=0
57 FILES=""
58
59 while [ $done -lt 100 ] ; do
60    echo "skipping $offset pics, getting 50 new pics to work on..."
61    # handle white space in filename correctly
62    FILES=`$NICE sqlite3 $LOCALDB "select replace(base_uri||'/'||filename,' ','%20') from photos limit $offset,100"`
63    
64    if [ "x$FILES" != "x" ] ; then
65      #found some files, process them
66      for file in $FILES; do
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 convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
76              if [ $removemeta=1 ]; then
77                  $NICE jhead -q -se -purejpg "Photos-tiny/$dir/$base"
78                  $NICE jhead -q -se -dt "Photos-tiny/$dir/$base"
79                  $NICE jhead -q -se -mkexif "Photos-tiny/$dir/$base"
80                  $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"
81              fi
82              done=$((done+1))
83          fi 
84          if [ ! -s "Photos-small/$dir/$base" ] ; then
85              mkdir -p Photos-small/$dir
86              $NICE convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
87              if [ $removemeta=1 ]; then
88                  $NICE jhead -q -se -purejpg "Photos-small/$dir/$base"
89                  $NICE jhead -q -se -dt "Photos-small/$dir/$base"
90                  $NICE jhead -q -se -mkexif "Photos-small/$dir/$base"
91                  $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"
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+50))
104 done