summaryrefslogtreecommitdiffstats
path: root/f-spot-db-reduce.sh
blob: 037b7835fd0c60497a325e4f80e79771c7008d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash

#    copyright 2012,2013 Arun Persaud <arun@nubati.net>
#
#    This file is part of photo-tags.
#
#    Photo-tags is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Photo-tags is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Photo-tags.  If not, see <http://www.gnu.org/licenses/>.

# parse ini file
CONFIG_FILE="config.ini"

# copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
    -e 's/;.*$//' \
    -e 's/\[.*\]//' \
    -e 's/[[:space:]]*$//' \
    -e 's/^[[:space:]]*//' \
    -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
   < $CONFIG_FILE `

LOCALDB=$fspotdb
REMOTEDB=$origdb
dbprefix=${dbprefix//\//\\\/}
# end parsing


function usage () {
    echo <<EOF
f-spot-db-reduce.sh <options>

The script uses config.ini for most of it's input data.

Options:
     --include=<comma separated taglist>      include pictures with these tags
     --exclude=<comma separated taglist>      remove pictures with these tags
     --hide=<comma separated taglist>         remove these tags, but leave pictures
EOF

}

# parse command line
CL_INCLUDE=""
CL_EXCLUDE=""
CL_HIDE=""

for i in "$@"; do
    case $i in
	--include=*)
	   CL_INCLUDE=${i#*=}
	   ;;
	--exclude=*)
	   CL_EXCLUDE=${i#*=}
	   ;;
	--hide=*)
	   CL_HIDE=${i#*=}
	   ;;
	-h|--help)
	   usage
	   ;;
    esac
done

# check database version, exit if different from what we expect
DBVERSION=`sqlite3 $REMOTEDB 'select data from meta where name="F-Spot Database Version"'`

if [ x"$DBVERSION" != "x18" ] ; then
    echo "The database is from a different F-spot version (database version $DBVERSION) that is not implemented."
    echo "Check TODO for a newer version or file a bug-report at: TODO"
    exit 2
fi


# join tags together in a list of comma seperated quoted strings, so that they can be used in DB queries
INCLUDE="'${CL_INCLUDE/,/','}'"
EXCLUDE="'${CL_EXCLUDE/,/','}'"
HIDE="'${CL_HIDE/,/','}'"

if [ "x$INCLUDE" != "x" ] ; then
    echo "only including pictures with tags: $INCLUDE"
fi

if [ "x$EXCLUDE" != "x" ] ; then
    echo "excluding all pictures with tags: $EXCLUDE"
fi

if [ "x$HIDE" != "x" ] ; then
    echo "removing tags: $HIDE"
fi

if [ "x$EXCLUDE" == "x" -a "x$INCLUDE" == "x" ] ; then
    echo "using all pictures from the database"
fi

# test if localdb already exist:
# yes:
#     see if photo-db is newer? yes: merge them; no: do nothing
# no:
#     create it

cp $REMOTEDB  $LOCALDB

ExcludeTags=""
if [ "x$EXCLUDE" != "x" ] ; then
    ExcludeTags="insert into rmphotoids select photo_id from photo_tags where tag_id in (select id from tags where name in ($EXCLUDE));"
fi

HideTags=""
if [ "x$HIDE" != "x" ] ; then
    HideTags="delete from photo_tags where tag_id in (select id from tags where name in ($HIDE));
              delete from tags where name in ($HIDE);"
fi

# delete all photo related information that we don't want to keep in case INCLUDE tags were given
sqlite3 $LOCALDB <<EOF
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)));

$ExcludeTags
$HideTags

delete from photos where id in (select * from rmphotoids);
delete from photo_tags where photo_id in (select * from rmphotoids);
delete from photo_versions where photo_id in (select * from rmphotoids);

delete from tags where id not in (select distinct tag_id from photo_tags asc);
delete from rolls where id not in (select distinct roll_id from photos asc);

drop table rmphotoids;
drop table jobs;
drop table exports;

vacuum;

EOF