#!/usr/bin/env python
# Script performs cvs checkout of a user-specified package and release number or HEAD,
# then creates RELEASE_SITE file based on user input
# 2010/11/29 dayle

import sys
import os
import subprocess
import optparse


__all__ = ['export_release_site_file', 'assemble_release_site_inputs','assemble_cvs_inputs']


def export_release_site_file(inputs):
    """
    Use the contents of a dictionary of top level dirs to create a 
    RELEASE_SITE dir in a specified dir
    """

    #out_file = sys.stdout for testing 

    output_file_and_path = './RELEASE_SITE'
    try:
        out_file = open(output_file_and_path, 'w')
    except IOError, e:
        sys.stderr.write('Could not open "%s": %s\n' % (output_file_and_path, e.strerror))
        return None

    print >> out_file, '#=============================================================================='
    print >> out_file, '#RELEASE Location of external products'
    print >> out_file, '# Run "gnumake clean uninstall install" in the application'
    print >> out_file, '# top directory each time this file is changed.'
    print >> out_file, ''
    print >> out_file, '#=============================================================================='
    print >> out_file, '# Define the top of the EPICS tree for your site.'
    print >> out_file, '# We will build some tools/scripts that allow us to'
    print >> out_file, '# change this easily when relocating software.'
    print >> out_file, '#=============================================================================='
    print >> out_file, 'EPICS_SITE_TOP=',inputs['EPICS_SITE_TOP'] 
    print >> out_file, 'BASE_SITE_TOP=',inputs['BASE_SITE_TOP']
    print >> out_file, 'MODULES_SITE_TOP=',inputs['MODULES_SITE_TOP']
    print >> out_file, 'IOC_SITE_TOP=',inputs['IOC_SITE_TOP']
    print >> out_file, '#=============================================================================='
    if out_file != sys.stdout:
        out_file.close()
    

def assemble_release_site_inputs():
    input_dict = {}
    input_dict['EPICS_SITE_TOP'] = None
    input_dict['BASE_SITE_TOP'] = None
    input_dict['MODULES_SITE_TOP'] = None
    input_dict['IOC_SITE_TOP'] = None
    #input_dict['DESTINATION_DIR']=None
    while not input_dict['EPICS_SITE_TOP']:
        input_dict['EPICS_SITE_TOP'] = raw_input('Enter full path for EPICS_SITE_TOP: ') 
    while not input_dict['BASE_SITE_TOP']:
        input_dict['BASE_SITE_TOP'] = raw_input('Enter full path for BASE_SITE_TOP: ')
    while not input_dict['MODULES_SITE_TOP']:
        input_dict['MODULES_SITE_TOP']= raw_input('Enter full path for MODULES_SITE_TOP: ')
    while not input_dict['IOC_SITE_TOP']:
        input_dict['IOC_SITE_TOP'] = raw_input('Enter full path for IOC_SITE_TOP: ')
    #while not input_dict['DESTINATION_DIR']:
    #    input_dict['DESTINATION_DIR'] = raw_input('Enter full or relative path for where I should create RELEASE_SITE. Enter \'.\' for cwd: ')
    return input_dict


def assemble_cvs_inputs():
    cvs_dict = {}
    cvs_dict['REPOSITORY'] = None
    cvs_dict['RELEASE'] = None
    while not cvs_dict['REPOSITORY']:
       cvs_dict['REPOSITORY'] = raw_input('Enter name of package to checkout: ')
    while not cvs_dict['RELEASE']:
       cvs_dict['RELEASE'] = raw_input('Enter name of tag or \'HEAD\' to checkout: ')

    # TO DO: does directory cvs_dict['REPOSITORY'] exist?
    if os.path.exists(cvs_dict['REPOSITORY']):
        print 'directory exists, just cd in'
    else:
        #os.mkdir(path[, mode]) add mode if you want...
        os.mkdir(cvs_dict['REPOSITORY'])

    os.chdir(cvs_dict['REPOSITORY'])
    os.system('pwd')
    cmd='cvs checkout -r '+cvs_dict['RELEASE']+' -d '+cvs_dict['RELEASE']+' '+cvs_dict['REPOSITORY']    
    print 'cvs checkout -r '+cvs_dict['RELEASE']+' -d '+cvs_dict['RELEASE']+' '+cvs_dict['REPOSITORY']    
    os.system(cmd)
    os.chdir(cvs_dict['RELEASE'])
    print 'cd '+cvs_dict['RELEASE']
    os.system('pwd')

def main(argv=None):
    assemble_cvs_inputs()
    # checkout the release and cd to the top level  
    inputs = assemble_release_site_inputs()
    export_release_site_file(inputs)
    return 0
    

if __name__ == '__main__':
    status = main()
    sys.exit(status)
