tune tagcloud a bit
[photo-tags.git] / f-spot-db-reduce.sh
1 #!/bin/bash
2
3 # parse ini file 
4 CONFIG_FILE="config.ini"
5
6 # copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
7 eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
8     -e 's/;.*$//' \
9     -e 's/\[.*\]//' \
10     -e 's/[[:space:]]*$//' \
11     -e 's/^[[:space:]]*//' \
12     -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
13    < $CONFIG_FILE `
14
15 LOCALDB=$fspotdb
16 REMOTEDB=$origdb
17 dbprefix=${dbprefix//\//\\\/}
18 # end parsing
19
20
21 function usage () {
22     echo "TODO: use comma seperate tags for --include=... and/or --exclude=..."
23 }
24
25 # parse command line
26 CL_INCLUDE=""
27 CL_EXCLUDE=""
28
29 for i in "$@"; do
30     case $i in 
31         --include=*)
32            CL_INCLUDE=${i#*=} 
33            ;;
34         --exclude=*)
35            CL_EXCLUDE=${i#*=} 
36            ;;
37         -h|--help)
38            usage
39            ;;
40     esac
41 done
42
43 # check database version, exit if different from what we expect
44 DBVERSION=`sqlite3 $REMOTEDB 'select data from meta where name="F-Spot Database Version"'`
45
46 if [ x"$DBVERSION" != "x18" ] ; then
47     echo "The database is from a different F-spot version (database version $DBVERSION) that is not implemented."
48     echo "Check TODO for a newer version or file a bug-report at: TODO"
49     exit 2
50 fi
51
52
53 # join tags together in a list of comma seperated quoted strings, so that they can be used in DB queries
54 INCLUDE="'${CL_INCLUDE/,/','}'"
55 EXCLUDE="'${CL_EXCLUDE/,/','}'"
56
57 if [ "x$INCLUDE" != "x" ] ; then
58     echo "only including pictures with tags: $INCLUDE"
59 fi
60
61 if [ "x$EXCLUDE" != "x" ] ; then
62     echo "excluding all pictures with tags: $EXCLUDE"
63 fi
64
65 if [ "x$EXCLUDE" == "x" -a "x$INCLUDE" == "x" ] ; then
66     echo "using all pictures from the database"
67 fi
68
69 # test if localdb already exist:
70 # yes:
71 #     see if photo-db is newer? yes: merge them; no: do nothing
72 # no: 
73 #     create it
74
75 cp $REMOTEDB  $LOCALDB
76
77 ExcludeTags=""
78 if [ "x$EXCLUDE" != "x" ] ; then
79     ExcludeTags="insert into rmphotoids select photo_id from photo_tags where tag_id in (select id from tags where name in ($EXCLUDE));"
80 fi
81
82 # delete all photo related information that we don't want to keep in case INCLUDE tags were given
83 sqlite3 $LOCALDB <<EOF
84 create temp table rmphotoids as select id from photos where id not in (select photo_id from photo_tags where tag_id in (select id from tags where name in ($INCLUDE)));
85
86 $ExcludeTags
87
88 delete from photos where id in (select * from rmphotoids);
89 delete from photo_tags where photo_id in (select * from rmphotoids);
90 delete from photo_versions where photo_id in (select * from rmphotoids);
91
92 delete from tags where id not in (select distinct tag_id from photo_tags asc);
93 delete from rolls where id not in (select distinct roll_id from photos asc);
94
95 drop table rmphotoids;
96 drop table jobs; 
97 drop table exports;
98
99 vacuum;
100
101 EOF