#!/usr/bin/python2.3
# arch-tag: program to initialize a Debian package repository
# Copyright (C) 2003 John Goerzen
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#

import os, sys
from tbppy import tla, extcmd
from tla_support.commandver import cmd

def printhelp():
    print """Usage:
%s archive-name archive-location working-location

Where:
  archive-name is the tla archive name to create; for instance,
  foo@example.com--debian

  archive-location is where the tla data gets stored; for instance,
  ~/arch/debian

  working-location is where you will check out sources and work with
  the system; for instance, ~/devel/debian""" % sys.argv[0]

def syntax():
    printhelp()
    sys.exit(1)

if len(sys.argv) < 4:
    syntax()

progname, ARCHIVENAME, ARCHIVELOC, WCLOC = sys.argv
CONFIGVER = "%s/configs--head--1.0" % ARCHIVENAME
if os.path.exists(ARCHIVELOC):
    print "Archive location %s already exists" % ARCHIVELOC
    sys.exit(2)
if os.path.exists(WCLOC):
    print "Working copy directory %s already exists" % WCLOC
    sys.exit(3)

print "Making working copy location..."
os.mkdir(WCLOC)
os.chdir(WCLOC)

print "Creating archive..."
extcmd.qrun('tla make-archive -l "%s" "%s"' % (ARCHIVENAME, ARCHIVELOC))

print "Initializing archive..."
extcmd.qrun('tla archive-setup "%s"' % CONFIGVER)

print "Initializing config tree..."
extcmd.qrun('tla init-tree "%s"' % CONFIGVER)
extcmd.qrun('tla %s tagline' % cmd().tagging_method)

print "Populating config tree..."
for newdir in ['configs', 'configs/upstream', 'configs/debian']:
    os.mkdir(newdir)
    extcmd.qrun('tla %s %s' % (' '.join(cmd().add), newdir))
    
print "Importing config tree..."
extcmd.qrun('tla import')

configfilename = os.path.expanduser('~/.tla-buildpackage')
if os.path.exists(configfilename):
    print "%s already exists; not modifying" % configfilename
else:
    print "Generating %s" % configfilename
    fd = open(configfilename, "w")
    fd.write("%s\n" % WCLOC)
    fd.close()

print "Operation successful."
