import java.sql.*;
import java.io.*;
import java.awt.Color;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.wrappers.*;
import uk.ac.ic.doc.automed.util.Debugger;

/*
 * An application to demonstrate how to setup models (i.e. modelling
 * languages such as ER, Relational, etc) using the
 * Automed API
 */

public class DefineRepository {
  // Generic reinitialise of a repository, to clear the main repositories,
  // call this twice, once with "MDR" and once with "STR"
  public static void initialiseRepository(String sourceName)
  throws DSRException, SQLException, IOException {
    String sa[]=new String[]{"",sourceName};

    DSR.doDropdbCommand(sa);
    DSR.doCreatedbCommand(sa);
    System.out.println("Repository "+sourceName+" initialised");
  }

  /**
   * dmle 11/3/2006: modified to accept -c flag which sets location of different 
   * data source configuration 
   * file
   */
  public static void main(String[] args) {
    String usage = "Usage: java -cp $CLASSPATH DefineRepository [-d \"m.n (...)\"] [-c cfgFile]" +
      "\n\n   -d  one or more set of debug options in format section.level (if more than one then must escape with double quotes)" +
      "\n   -c  full path to the customised data source configuration file (default: $HOME/.automed/data_source_repository.cfg)" +
      "\n\n Examples:" +
      "\n java DefineRepository -d 0.3 " +
      "\n java DefineRepository -d \"0.3 2.3\" -c $HOME/.automed/data_source_repository_1.cfg";
    
    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-h")) {
        System.out.println(usage);
        System.exit(0);
      }        
    }
    
    
    try {
      System.out.println("AutoMed Repository Reinitialisation");

      //STR.setDebugLevel(0);
      Debugger.setSectionDebugLevel(args);
      DSRConfiguration.setDefaultConfigFileName(args);
      
      initialiseRepository(MDR.getDataSourceName());      
      initialiseRepository(STR.getDataSourceName());
//      initialiseRepository(SchemaModelingRepository.getDataSourceName());
//      initialiseRepository(TemplateExecutionRepository.getDataSourceName());
 //     initialiseRepository(TemplateTransformationRepository.getDataSourceName());
  //    initialiseRepository(TemplateTransformationSyntaxRepository.getDataSourceName());
      
      /*dmle 6/12/2005: create HDM models and its types */
      System.out.println("Intialising HDM model...");
      HDMWrapperFactory hdmf = new HDMWrapperFactory();
      hdmf.createModel();      
    }
    catch (SQLException se) { 
      se.printStackTrace();
    }
    catch (IOException ie) {
      ie.printStackTrace();
    } 
    catch (AutoMedException ame) {
      ame.printStackTrace();
    }      
  }
}

























