#! /usr/bin/env python

# Copyright (C) 2008 by Sharif Oerton 
# sharif@oerton.net.au
#  
# This program is free software: you can redistribute it and/or 
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of 
# the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.*)

import socket
import pickle
import os
import sys

import grimoire

PORT = 1111

grimoire.init()

def print_options():
  print '''Options are:\n
	-a: add new entry
	-r <firstname> <lastname>: remove entry
	-m <firstname> <lastname>: modify entry
	-g <firstname> <lastname>: retrieve entry
	-i: show all entries
	-s <IP address>: sync with another computer
	-l <IP address>: listen for syncing connections from a particular computer'''

if len(sys.argv) < 2:
  grimoire.print_options()
  sys.exit()

if sys.argv[1] == "-a":
  fn = raw_input("Enter first name: ")
  ln = raw_input("Enter last name: ")
  email1 = raw_input("Enter email 1: ")
  email2 = raw_input("Enter email 2: ")
  add1 = raw_input("Enter address 1: ")
  add2 = raw_input("Enter address 2: ")
  city = raw_input("Enter city: ")
  state = raw_input("Enter state: ")
  postcode = raw_input("Enter postcode: ")
  country = raw_input("Enter country: ")
  homephone = raw_input("Enter home phone: ")
  workphone = raw_input("Enter work phone: ")
  fax = raw_input("Enter fax: ")
  mobile = raw_input("Enter mobile: ")

  grimoire.add_record(fn, ln, email1, email2, add1, add2, city, state, postcode, country, homephone, workphone, fax, mobile)

elif sys.argv[1] == "-r":
  if len(sys.argv) == 4:
    grimoire.del_record(sys.argv[2], sys.argv[3])
  else:
    print_options()

elif sys.argv[1] == "-m":
  if len(sys.argv) == 4:
    email1 = raw_input("Enter email 1: ")
    email2 = raw_input("Enter email 2: ")
    add1 = raw_input("Enter address 1: ")
    add2 = raw_input("Enter address 2: ")
    city = raw_input("Enter city: ")
    state = raw_input("Enter state: ")
    postcode = raw_input("Enter postcode: ")
    country = raw_input("Enter country: ")
    homephone = raw_input("Enter home phone: ")
    workphone = raw_input("Enter work phone: ")
    fax = raw_input("Enter fax: ")
    mobile = raw_input("Enter mobile: ")

    grimoire.mod_record(sys.argv[2], sys.argv[3], email1, email2, add1, add2, city, state, postcode, country, homephone, workphone, fax, mobile)
  else:
    print_options()

elif sys.argv[1] == "-s":
  if len(sys.argv) == 3:
    print "Syncing..."
    grimoire.sync(sys.argv[2])
    print "Syncing complete."
  else:
    print_options()

elif sys.argv[1] == "-l":
  if len(sys.argv) == 3:
    print "Listening..."
    grimoire.serve(sys.argv[2])
    print "Transfer complete."
  else:
    print_options()

elif sys.argv[1] == "-g":
  if len(sys.argv) == 4:
    print grimoire.get_record(sys.argv[2], sys.argv[3])
  else:
    print_options()
elif sys.argv[1] == "-i":
  for rec in grimoire.list_records():
    print rec

else:
  print_options()

