#! /usr/bin/env python
########################################################################
# COPYRIGHT_BEGIN
#
#           mical
#           Minimal iCalendar utilities
#
# Copyright (C) 2007, David Arnold
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# COPYRIGHT_END
########################################################################
# $ZeroXOne: mical/micsetup,v 1.2 2007/04/26 05:53:40 d Exp $
########################################################################

import os, sys

PROGRAM = os.path.basename(sys.argv[0])
VERSION = "1.0.0"

########################################################################

def usage(msg=None):
    if msg:
        print msg
    print "Usage: %s email" % PROGRAM
    return


def main():
    # Check options
    if len(sys.argv) != 2:
        usage("Insufficient options")
        sys.exit(1)

    email = sys.argv[1]
    if email.find("@") < 1 or email.find(".") < 0 or len(email) < 6:
        usage("Bad format for email address")
        sys.exit(1)

    try:
        home = os.environ["HOME"]
    except KeyError:
        usage("Unable to find HOME environment variable")
        sys.exit(1)

    if not os.path.exists(home):
        usage("Unable to find home directory")
        sys.exit(1)
    
    # Create directory
    try:
        os.mkdir("%s/.mical" % home)
    except OSError:
        usage("Unable to create $HOME/.mical directory")
        sys.exit(1)

    # Create last
    try:
        f = open("%s/.mical/last" % home, "w")
        f.write("0\n")
        f.close()
    except IOError:
        usage("Unable to create last index file $HOME/.mical/last")
        sys.exit(1)
        
    # Create me
    try:
        f = open("%s/.mical/me" % home, "w")
        f.write(email+"\n")
        f.close()
    except IOError:
        usage("Unable to create configuration file $HOME/.mical/me")
        sys.exit(1)

    print "Done"
    return


if __name__ == "__main__":
    main()
    sys.exit(0)


########################################################################
