updated copyright year on webpage
[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 <<EOF
23 f-spot-db-reduce.sh <options>
24
25 The script uses config.ini for most of it's input data.
26
27 Options:
28      --include=<comma separated taglist>      include pictures with these tags
29      --exclude=<comma separated taglist>      remove pictures with these tags
30      --hide=<comma separated taglist>         remove these tags, but leave pictures
31 EOF
32
33 }
34
35 # parse command line
36 CL_INCLUDE=""
37 CL_EXCLUDE=""
38 CL_HIDE=""
39
40 for i in "$@"; do
41     case $i in
42         --include=*)
43            CL_INCLUDE=${i#*=}
44            ;;
45         --exclude=*)
46            CL_EXCLUDE=${i#*=}
47            ;;
48         --hide=*)
49            CL_HIDE=${i#*=}
50            ;;
51         -h|--help)
52            usage
53            ;;
54     esac
55 done
56
57 # check database version, exit if different from what we expect
58 DBVERSION=`sqlite3 $REMOTEDB 'select data from meta where name="F-Spot Database Version"'`
59
60 if [ x"$DBVERSION" != "x18" ] ; then
61     echo "The database is from a different F-spot version (database version $DBVERSION) that is not implemented."
62     echo "Check TODO for a newer version or file a bug-report at: TODO"
63     exit 2
64 fi
65
66
67 # join tags together in a list of comma seperated quoted strings, so that they can be used in DB queries
68 INCLUDE="'${CL_INCLUDE/,/','}'"
69 EXCLUDE="'${CL_EXCLUDE/,/','}'"
70 HIDE="'${CL_HIDE/,/','}'"
71
72 if [ "x$INCLUDE" != "x" ] ; then
73     echo "only including pictures with tags: $INCLUDE"
74 fi
75
76 if [ "x$EXCLUDE" != "x" ] ; then
77     echo "excluding all pictures with tags: $EXCLUDE"
78 fi
79
80 if [ "x$HIDE" != "x" ] ; then
81     echo "removing tags: $HIDE"
82 fi
83
84 if [ "x$EXCLUDE" == "x" -a "x$INCLUDE" == "x" ] ; then
85     echo "using all pictures from the database"
86 fi
87
88 # test if localdb already exist:
89 # yes:
90 #     see if photo-db is newer? yes: merge them; no: do nothing
91 # no:
92 #     create it
93
94 cp $REMOTEDB  $LOCALDB
95
96 ExcludeTags=""
97 if [ "x$EXCLUDE" != "x" ] ; then
98     ExcludeTags="insert into rmphotoids select photo_id from photo_tags where tag_id in (select id from tags where name in ($EXCLUDE));"
99 fi
100
101 HideTags=""
102 if [ "x$HIDE" != "x" ] ; then
103     HideTags="delete from photo_tags where tag_id in (select id from tags where name in ($HIDE));
104               delete from tags where name in ($HIDE);"
105 fi
106
107 # delete all photo related information that we don't want to keep in case INCLUDE tags were given
108 sqlite3 $LOCALDB <<EOF
109 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)));
110
111 $ExcludeTags
112 $HideTags
113
114 delete from photos where id in (select * from rmphotoids);
115 delete from photo_tags where photo_id in (select * from rmphotoids);
116 delete from photo_versions where photo_id in (select * from rmphotoids);
117
118 delete from tags where id not in (select distinct tag_id from photo_tags asc);
119 delete from rolls where id not in (select distinct roll_id from photos asc);
120
121 drop table rmphotoids;
122 drop table jobs;
123 drop table exports;
124
125 vacuum;
126
127 EOF