Update nice level and import interval
[iPhone2Mac.git] / iPhone2Mac.sh
1 #!/bin/bash
2 #
3 # iPhone2Mac -- save call logs from iPhone backups
4 # Copyright (c)2009-2011 Alexander Barton (alex@barton.de).
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING and README for more information.
11 #
12
13 DEBUG=
14 VERBOSE=1
15
16 umask 0077
17
18 [ -n "$DEBUG" ] && VERBOSE=1
19
20 abort() {
21         echo "$*"
22         exit 1
23 }
24
25 message() {
26         echo "$*"
27 }
28 verbose() {
29         [ -n "$VERBOSE" ] && message "$*"
30 }
31 debug() {
32         [ -n "$DEBUG" ] && verbose "$*"
33 }
34
35 import_d() {
36         call_db=""
37         cd "$1" || abort "Can't change directory to \"$1\"!"
38         verbose "iPhone Backup directory is \"$1\"."
39         if [ -z "$call_db" ]; then
40                 verbose "Locating SQLite call database(s) ..."
41                 call_db=$(for f in `file * | fgrep SQLite | cut -d':' -f1`; do
42                         sqlite3 "$f" ".schema" | fgrep "CREATE TABLE call (" >/dev/null;
43                         [ $? -eq 0 ] && echo "$f"; done
44                 )
45         fi
46         for f in $call_db; do
47                 verbose "Found call database: \"$f\""
48         done
49
50         if [ -e "$CALL_LOG" ]; then
51                 debug "Copying existing ASCII call log ..."
52                 cp "$CALL_LOG" "$CALL_TMP" || abort "Can't copy old log file!"
53         fi
54         for f in $call_db; do
55                 verbose "Dumping SQLite call databse \"$f\" ..."
56                 sqlite3 "$f" "select * from call;" | while read row; do
57                         IFS="|"
58                         arr=($row)
59                         unset IFS
60
61                         rowid=${arr[0]}
62                         address=${arr[1]}
63                         date=${arr[2]}
64                         duration=${arr[3]}
65                         flags=${arr[4]}
66                         id=${arr[5]}
67
68                         echo "$date|$rowid|$address|$duration|$flags|$id" \
69                           >>"$CALL_TMP"
70                 done
71         done
72         verbose "Sorting plain ASCII call log ..."
73         sort -un "$CALL_TMP" >"$CALL_LOG"
74         rm -f "$CALL_TMP"
75         first_d=$(date -r `head -1 "$CALL_LOG" | cut -d'|' -f1` "+%Y-%m-%d %H:%M")
76         last_d=$(date -r `tail -1 "$CALL_LOG" | cut -d'|' -f1` "+%Y-%m-%d %H:%M")
77         message `wc -l "$CALL_LOG" | awk '{print \$1}'`" unique entries logged, from $first_d to $last_d."
78 }
79
80 NAME=`basename "$0" | cut -d'.' -f1`
81 DATA_DIR=~/Library/Application\ Support/$NAME
82 CALL_LOG="${DATA_DIR}/call.log"
83 CALL_TMP="/tmp/$NAME.$$.call.log.tmp"
84 debug "DATA_DIR=$DATA_DIR"
85
86 mkdir -p "$DATA_DIR" || abort "Can't create \"$DATA_DIR\"!"
87
88 verbose "Locating backup directories ..."
89 [ -d ~/Library/Application\ Support/MobileSync/Backup ] \
90         || abort "No backup data stores found!?"
91
92 for d in ~/Library/Application\ Support/MobileSync/Backup/*; do
93         import_d "$d"
94 done