#!/bin/sh
# Copyright (c) 2010, Aldis Berjoza <aldis@bsdroot.lv>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name of the nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
address_book="$HOME/.ashbook"
save_backup=1 # saves backup when removing record if set
awk_output_format='
{
split($0, f, "|");
if( f[1] ) print " name: " f[1];
if( f[2] ) print " surname: " f[2];
if( f[3] ) print " email: " f[3];
if( f[4] ) print " phone: " f[4];
if( f[5] ) print " address: " f[5];
if( f[6] ) print " date: " f[6];
if( f[7] ) print " homepage: " f[7];
if( f[8] ) print " code: " f[8];
if( f[9] ) print " comment: " f[9];
print "";
}'
if [ ! -f $address_book ]; then
touch $address_book
if [ $? -ne 0 ]; then
echo "ERR: can't create file $address_book" >&2
exit 1
fi
fi
help() {
version=1.0
prog_name=$(basename $0)
cat >&2 << EOF
$prog_name v$version is a very simple addressbook written in sh
$prog_name usage:
$prog_name {add | find | rm} key1=value1 [key2=value2 [...]]
$prog_name list
Where key can be any of are:
name, surname, code, phone, address, position, email, homepage, date,
comment
Comment key is special and must be last key used. Comment value may
contain spaces.
Values and commands are case sensetive, keys are not.
Use of | character is forbidden as it's used as field seperator in
addressbook.
EOF
exit 1
}
if [ "$1" = 'add' ]; then
func='add'
elif [ "$1" = 'find' ]; then
func='find'
elif [ "$1" = 'list' ]; then
awk "$awk_output_format" $address_book
exit 0
elif [ "$1" = 'rm' ]; then
func='rm'
else
help
fi
shift
if [ $# -eq 0 ]; then
help
exit 1
fi
while [ $# -gt 0 ]; do
key=$(echo $1 | sed 's/=.*//' | tr 'QWERTYUIOPASDFGHJKLZXCVBNM' 'qwertyuiopasdfghjklzxcvbnm')
value=$(echo $1 | sed 's/.*=//')
case $key in
'name' | 'surname' | 'code' | 'phone' | 'address' | 'position' | 'email' | 'date' | 'homepage' )
eval `echo "$key=$value"`;
;;
'comment' )
comment=$(echo $* | sed 's/.*=//')
break
;;
*)
echo "ERR: key '$key' invalid. Ignoring" >&2
;;
esac
shift
done
find_record="${name:-.*}|${surname:-.*}|${email:-.*}|${phone:-.*}|${address:-.*}|${date:-.*}|${homepage:-.*}|${code:-.*}|${comment:-.*}"
if [ $func = 'add' ]; then
if [ $(grep -e "$find_record" $address_book) ]; then
echo 'ERR: looks like this record already exists' >&2
exit 1
fi;
echo "$name|$surname|$email|$phone|$address|$date|$homepage|$code|$comment" >> $address_book
echo "INFO: Record added"
exit 0
elif [ $func = 'find' ]; then
grep -e "$find_record" -x $address_book | awk "$awk_output_format"
exit 0
elif [ $func = 'rm' ]; then
found=`grep -e "$find_record" -x $address_book`
if [ "$found" != "" ]; then
echo "$found" | awk "$awk_output_format"
echo "Do you really want to delete above entries? [y|n]"
read delete
if [ "$delete" = 'y' ] || [ "$delete" = 'Y' ]; then
cp -f $address_book ${address_book}.bak
grep -e "$find_record" -x -v ${address_book}.bak > ${address_book}
if [ ! $save_backup ]; then
rm -f ${address_book}.bak
fi
echo
echo "INFO: Record(s) deleted"
fi
else
echo "INFO: No match found"
fi
exit 0
fi
exit 1
# vim:tabstop=4:shiftwidth=4: