1#!/bin/sh
2# (c) 2005 Daniel Kercher
3# Script to sync the ipod with abook
4
5# Usage:
6#
7# sync-abook.sh [-i <ipod mountpoint>] [-d <abook database>]
8#           ... [-e <encoding ipod>] [-f <encoding abook>]
9#
10# with the following defaults:
11
12IPOD_MOUNT='/mnt/ipod'				# mount point of ipod
13DATAFILE='~/.abook/addressbook'	                # the abook db
14ENCODING_FROM=UTF-8                             # encoding used by abook
15ENCODING=ISO-8859-15                            # encoding used by ipod
16
17# Unless called with "-e=none" this script requires "iconv" available
18# from http://www.gnu.org/software/libiconv/
19
20# About the encoding used by the iPod (by Jorg Schuler):
21#
22# For some reason the encoding used for the contact files and
23# calenader files depends on the language you have set your iPod
24# to. If you set your iPod to German, iso-8859-15 (or -1?) is
25# expected. If you set it to Japanese, SHIFT-JIS is expected. You need
26# to reboot the iPod to have the change take effect, however. (I'm
27# using firmware version 1.3.)
28#
29# If you know of more encodings, please let me know, so they can be
30# added here:
31#
32# iPod language      encoding expected
33# ----------------------------------------
34# German             ISO-8859-15
35# Japanese           SHIFT-JIS
36
37# Changelog
38# 2005/06/15 (Jorg Schuler <jcsjcs at users dot sourceforge dot net>):
39# Received original script from Daniel Kercher and added character
40# conversion and command line options.
41#
42# 2007/05/31 (Jorg Schuler <jcsjcs at users dot sourceforge dot net>):
43# Set a more reasonable default datafile.
44#
45
46# overwrite default settings with optional command line arguments
47while getopts i:d:e:f: option; do
48    case $option in
49        i) IPOD_MOUNT=$OPTARG;;
50        d) DATAFILE=$OPTARG;;
51        e) ENCODING=$OPTARG;;
52	f) ENCODING_FROM=$OPTARG;;
53        \?) echo "Usage: `basename $0` [-i <ipod mountpoint>] [-d <abook database>] [-e <encoding ipod>] [-f <encoding abook>]"
54	    exit 1;;
55    esac
56done
57
58# set the RECODE command
59if [ $ENCODING = "none" ] || [ $ENCODING = "NONE" ]; then
60    RECODE="cat"    # no conversion
61else
62    RECODE="iconv -f $ENCODING_FROM -t $ENCODING"
63fi
64
65
66# check if DATAFILE exists
67if [ ! -f $DATAFILE ]; then
68    echo "Error: $DATAFILE does not exist"
69    exit 1
70fi
71
72echo -n "Syncing abook to iPod... "
73abook --convert --infile $DATAFILE --outformat gcrd | sed '/^$/d' | $RECODE > ${IPOD_MOUNT}/Contacts/abook.vcf
74echo "done!"
75