]> arthur.barton.de Git - iPhone2Mac.git/blob - iPhone2Mac.sh
Add proper copyright notice: GNU GPL
[iPhone2Mac.git] / iPhone2Mac.sh
1 #!/bin/bash
2 #
3 # iPhone2Mac -- save call logs from iPhone backups
4 # Copyright (c)2009 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 backup_d=""
17 call_db=""
18
19 [ -n "$DEBUG" ] && VERBOSE=1
20
21 abort() {
22         echo "$*"
23         exit 1
24 }
25
26 message() {
27         echo "$*"
28 }
29 verbose() {
30         [ -n "$VERBOSE" ] && message "$*"
31 }
32 debug() {
33         [ -n "$DEBUG" ] && verbose "$*"
34 }
35
36 umask 0077
37
38 NAME=`basename "$0" | cut -d'.' -f1`
39 DATA_DIR=~/Library/Application\ Support/$NAME
40 CALL_LOG="${DATA_DIR}/call.log"
41 CALL_TMP="/tmp/$NAME.$$.call.log.tmp"
42 debug "DATA_DIR=$DATA_DIR"
43
44 mkdir -p "$DATA_DIR" || abort "Can't create \"$DATA_DIR\"!"
45
46 if [ -z "$backup_d" ]; then
47         verbose "Locating backup directory ..."
48         cd ~/Library/Application\ Support/MobileSync/Backup/ \
49                 || abort "No backup data stores found!?"
50         cd * \
51                 || abort "Can't access backup directory!?"
52 else
53         cd "$backup_d" || abort "Can't access backup directory!?"
54 fi
55 verbose "iPhone Backup directory is \"$PWD\"."
56
57 if [ -z "$call_db" ]; then
58         verbose "Locating SQLite call database(s) ..."
59         call_db=$(for f in `file * | fgrep SQLite | cut -d':' -f1`; do
60                 sqlite3 "$f" ".schema" | fgrep "CREATE TABLE call (" >/dev/null;
61                 [ $? -eq 0 ] && echo "$f"; done
62         )
63 fi
64 for f in $call_db; do
65         verbose "Found call database: \"$f\""
66 done
67
68 if [ -e "$CALL_LOG" ]; then
69         debug "Copying existing ASCII call log ..."
70         cp "$CALL_LOG" "$CALL_TMP" || abort "Can't copy old log file!"
71 fi
72 for f in $call_db; do
73         verbose "Dumping SQLite call databse \"$f\" ..."
74         sqlite3 "$f" "select * from call;" >>"$CALL_TMP"
75 done
76 verbose "Sorting plain ASCII call log ..."
77 sort -u "$CALL_TMP" >"$CALL_LOG"
78 rm -f "$CALL_TMP"
79 first_d=$(date -r `head -1 "$CALL_LOG" | cut -d'|' -f3` "+%Y-%m-%d %H:%M")
80 last_d=$(date -r `tail -1 "$CALL_LOG" | cut -d'|' -f3` "+%Y-%m-%d %H:%M")
81 message `wc -l "$CALL_LOG" | awk '{print \$1}'`" unique entries logged, from $first_d to $last_d."