summaryrefslogtreecommitdiffstats
path: root/f-spot-db-reduce.sh
blob: fc421feca89c6b452d8a1edbef0f3043093bfad5 (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
#!/bin/bash

# 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 "TODO: use comma seperate tags for --include=... and/or --exclude=..."
}

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

for i in "$@"; do
    case $i in 
	--include=*)
	   CL_INCLUDE=${i#*=} 
	   ;;
	--exclude=*)
	   CL_EXCLUDE=${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/,/','}'"

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$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

# 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

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