#!/bin/bash
#convert outlook CSV adddressbook to some other format
#version 0.1 - 18-03-2002


typeset -i MAXLEN_NAME=25
typeset -i MAXLEN_EMAIL=40
typeset -i MAXLEN_PHONE=29
typeset -i MAXLEN_BIRTHDAY=10

#------------ DONT CHANGE BELOW

if [ $# != 1 ]; then
 echo "convert outlook CSV adddressbook to own addressformat for printing"
 echo "Syntax: $0 filename" 
 exit 1
fi
FILE=$1
typeset -i FLAG=1

#Title,First Name,Middle Name,Last Name,Suffix,Company,Department,Business Street,Business Street 2,Business Street 3 (10) ,Business City,Business State,Business Postal Code,Business Country,Home Street,Home City,Home State,Home Postal Code,Home Country,Business Fax (20) ,Business Phone,Assistant's Phone,Car Phone,ISDN,Home Phone (25) ,Mobile Phone,Pager,Business Phone 2,Birthday (29) ,E-mail Address,E-mail Address 2,Web Page,Notes

while read line
do 
#echo $line

NAME=`echo $line | cut --output-delimiter=\  -d, -f2\ 4`
typeset -i NAMELEN=`echo $NAME | wc -c`
EMAIL=`echo $line | cut -d, -f30`
typeset -i EMAILLEN=`echo $EMAIL | wc -c`
PHONE=`echo $line | cut -d, -f25`
typeset -i PHONELEN=`echo $PHONE | wc -c`
MOBILEPHONE=`echo $line | cut -d, -f26`
typeset -i MOBILEPHONELEN=`echo $MOBILEPHONE | wc -c`
BIRTHDAY=`echo $line | cut -d, -f29`

echo -n "NAME: $NAME"
counter=0
let "numspaces = $MAXLEN_NAME-$NAMELEN"
while [ "$counter" -lt $numspaces ]
 do
   if [ $FLAG -eq 0 ]; then
    echo -n " "
   else
    echo -n "_"
   fi
   let "counter += 1"
 done

 echo -n "PHONE: $PHONE $MOBILEPHONE"
counter=0
let "numspaces = $MAXLEN_PHONE-$PHONELEN-$MOBILEPHONELEN"
while [ "$counter" -lt $numspaces ]
 do
   if [ $FLAG -eq 0 ]; then
    echo -n " "
   else
    echo -n "_"
   fi
   let "counter += 1"
 done

echo -n "EMAIL: $EMAIL"
counter=0
let "numspaces = $MAXLEN_EMAIL-$EMAILLEN"
while [ "$counter" -lt $numspaces ]
 do
   if [ $FLAG -eq 0 ]; then
    echo -n " "
   else
    echo -n "_"
   fi
   let "counter += 1"
 done

echo -n "BIRTHDAY: $BIRTHDAY"
echo
if [ $FLAG -eq 0 ]; then
  FLAG=1
 else
  FLAG=0
fi

done < $1

echo "a2ps -r --columns=1 -B --chars-per-line=132 output.tmp"

