Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision 7151c4d1599615f43467a66e761b7bd1821684ca)
+++ src/builder.cpp	(revision dbe929ff6db6c4da014f56f595de7f279f3f76e3)
@@ -683,10 +683,54 @@
 };
 
+/** Tries given filename or standard on saving the config file.
+ * \param *ConfigFileName name of file
+ * \param *configuration pointer to configuration structure with all the values
+ * \param *periode pointer to periodentafel structure with all the elements
+ * \param *mol pointer to molecule structure with all the atoms and coordinates
+ */
+void SaveConfig(char *ConfigFileName, config *configuration, periodentafel *periode, molecule *mol)
+{
+  char filename[255];
+  ofstream output;
+
+  // get correct valence orbitals
+  mol->CalculateOrbitals(*configuration);
+  configuration->InitMaxMinStopStep = configuration->MaxMinStopStep = configuration->MaxPsiDouble;
+  if (ConfigFileName != NULL)
+    output.open(ConfigFileName, ios::trunc);
+  if (output == NULL)
+    output.open("main_pcp_linux", ios::trunc);
+  if (configuration->Save(&output, periode, mol))
+    cout << Verbose(0) << "Saving of config file successful." << endl;
+  else
+    cout << Verbose(0) << "Saving of config file failed." << endl;
+  output.close();
+  output.clear();
+  // and save to xyz file
+  if (ConfigFileName != NULL) {
+    strcpy(filename, ConfigFileName);
+    strcat(filename, ".xyz");
+    output.open(filename, ios::trunc);
+  }
+  if (output == NULL) {
+    strcpy(filename,"main_pcp_linux");
+    strcat(filename, ".xyz");
+    output.open(filename, ios::trunc);
+  } 
+  if (mol->OutputXYZ(&output))
+    cout << Verbose(0) << "Saving of XYZ file successful." << endl;
+  else
+    cout << Verbose(0) << "Saving of XYZ file failed." << endl;
+  output.close();
+  output.clear();
+};
+
 /********************************************** Main routine **************************************/
- 
+
 int main(int argc, char **argv)
 {
   periodentafel *periode = new periodentafel; // and a period table of all elements
   molecule *mol = new molecule(periode);    // first we need an empty molecule
+  config configuration;
   double tmp1;
   double bond, min_bond;
@@ -697,9 +741,9 @@
 	double *factor;	// unit factor if desired
   bool valid; // flag if input was valid or not
-  config configuration;
   ifstream test;
   ofstream output;
   string line;
   char filename[255];
+  char *ConfigFileName = NULL;
   int flag = 1;
   int Z;
@@ -708,4 +752,5 @@
   enum BondOrderScheme Scheme = NoScheme;
   enum CutCyclicBond CutCyclic;
+  enum ConfigStatus config_present = absent;
   MoleculeLeafClass *Subgraphs = NULL;
   clock_t start,end;
@@ -713,8 +758,4 @@
   vector **Vectors;
 
-  // Greetings
-  cout << Verbose(0) << "Molecuilder v1" << endl << endl;
-  cout << Verbose(0) << "Build your own molecule position set." << endl;
-
   // load element list
   periode->LoadPeriodentafel();
@@ -722,183 +763,218 @@
   
   // parse config file if given
-  if (argc >= 2) { // config file specified as option
-  	cout << Verbose(0) << "Config file given." << endl;
-    test.open(argv[1], ios::in);
-    if (test == NULL) {
-      cout << Verbose(1) << "Specified config file not found." << endl;
-      //return (1);
-    } else {
-      cout << Verbose(1) << "Specified config file found, parsing ...";
-      switch (configuration.TestSyntax(&test, periode, mol)) {
-        case 1:
-          cout << "new syntax." << endl;
-          configuration.Load(&test, periode, mol);
-          break;
-        case 0:
-          cout << "old syntax." << endl;
-          configuration.LoadOld(&test, periode, mol);
-          break;
-        default:
-          cout << "unknown syntax or empty configuration file." << endl;
-          //return(1);
+  if (argc >= 1) { // config file specified as option
+  	if (argv[argc-1][0] != '-') {
+      cout << Verbose(0) << "Config file given." << endl;
+      test.open(argv[argc-1], ios::in);
+      if (test == NULL) {
+        //return (1);
+        output.open(argv[argc-1], ios::out);
+        if (output == NULL) {
+          cout << Verbose(1) << "Specified config file " << argv[argc-1] << " not found." << endl;
+          config_present = absent;
+        } else {
+          cout << "Empty configuration file." << endl;
+          ConfigFileName = argv[argc-1];
+          config_present = empty;
+          output.close();
+        }
+      } else {
+        ConfigFileName = argv[argc-1];
+        cout << Verbose(1) << "Specified config file found, parsing ...";
+        switch (configuration.TestSyntax(&test, periode, mol)) {
+          case 1:
+            cout << "new syntax." << endl;
+            configuration.Load(&test, periode, mol);
+            config_present = present;
+            break;
+          case 0:
+            cout << "old syntax." << endl;
+            configuration.LoadOld(&test, periode, mol);
+            config_present = present;
+            break;
+          default:
+            cout << "Unknown syntax or empty, yet present file." << endl;
+            config_present = empty;
+       }
+        test.close();
       }
-      test.close();
-    }
-  }
-  // command line stuff
-  if (argc > 2) {
-    cout << Verbose(0) << "Recognized command line argument: " << argv[2][1] << ".\n";
-    switch(argv[2][1]) {
-      case 't':
-        cout << Verbose(1) << "Translating all ions to new origin." << endl;
-        for (int i=0;i<3;i++)
-          x.x[i] = atof(argv[3+i]);
-        mol->Translate((const vector *)&x);
-        break;
-      case 'a':
-        cout << Verbose(1) << "Adding new atom." << endl;
-        first = new atom;
-        for (int i=0;i<3;i++)
-          first->x.x[i] = atof(argv[4+i]);
-        finder = periode->start;
-        while (finder != periode->end) {
-          finder = finder->next;
-          if (strncmp(finder->symbol,argv[3],3) == 0) {
-            first->type = finder;
+  	} else
+  	  config_present = absent;
+  	int argptr = 1;
+  	do {
+  	  if (argv[argptr][0] == '-') {
+        cout << Verbose(0) << "Recognized command line argument: " << argv[argptr][1] << ".\n";
+        argptr++;
+        switch(argv[argptr-1][1]) {
+          case 'h':
+          case 'H':
+          case '?':
+            cout << "MoleCuilder suite" << endl << "==================" << endl << endl;
+            cout << "Usage: " << argv[0] << "[-{acsthH?vfrp}] [further arguments] [config file]" << endl; 
+            cout << "or simply " << argv[0] << " without arguments for interactive session." << endl;
+            cout << "\t-a Z x1 x2 x3\tAdd new atom of element Z at coordinates (x1,x2,x3)." << endl;
+            cout << "\t-c x1 x2 x3\tCenter atoms in domain with a minimum distance to boundary of (x1,x2,x3)." << endl;
+            cout << "\t-h/-H/-?\tGive this help screen." << endl;
+            cout << "\t-p <file>\tParse given xyz file and create raw config file from it." << endl;
+            cout << "\t-r\t\tConvert file from an old pcp syntax." << endl;
+            cout << "\t-t x1 x2 x3\tTranslate all atoms by this vector (x1,x2,x3)." << endl;
+            cout << "\t-s x1 x2 x3\tScale all atom coordinates by this vector (x1,x2,x3)." << endl;
+            cout << "\t-v/-V\t\tGives version information." << endl;
+            return 0;
             break;
+          case 'v':
+          case 'V':
+            cout << argv[0] << " " << VERSIONSTRING << endl;
+            cout << "Build your own molecule position set." << endl;
+            return 0;
+            break;
+          default:    
+            break;
+        }
+        if ((config_present == present) || (config_present == empty)) {
+          switch(argv[argptr-1][1]) {
+            case 'p':
+              cout << Verbose(1) << "Parsing xyz file for new atoms." << endl;
+              if (!mol->AddXYZFile(argv[argptr++]))
+                cout << Verbose(2) << "File not found." << endl;
+              else
+                cout << Verbose(2) << "File found and parsed." << endl;
+              break;
+            default:
+              break;
           }
         }
-        mol->AddAtom(first);  // add to molecule
-        break;
-      case 's':
-        j = -1;
-        cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
-        factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor");
-        factor[0] = atof(argv[3]);
-        factor[1] = (argc >=4) ? atof(argv[4]) : atof(argv[3]); 
-        factor[2] = (argc >=5) ? atof(argv[5]) : atof(argv[3]); 
-        mol->Scale(&factor);
-        for (int i=0;i<3;i++) {
-          j += i+1;
-          x.x[i] = atof(argv[3+i]);
-          mol->cell_size[j]*=factor[i];
+        if (config_present != empty) { 
+          if (config_present == present) {
+            switch(argv[argptr-1][1]) {
+              case 't':
+                cout << Verbose(1) << "Translating all ions to new origin." << endl;
+                for (int i=0;i<3;i++)
+                  x.x[i] = atof(argv[argptr+i]);
+                mol->Translate((const vector *)&x);
+                argptr+=3;
+                break;
+              case 'a':
+                cout << Verbose(1) << "Adding new atom." << endl;
+                first = new atom;
+                for (int i=0;i<3;i++)
+                  first->x.x[i] = atof(argv[argptr+1+i]);
+                finder = periode->start;
+                while (finder != periode->end) {
+                  finder = finder->next;
+                  if (strncmp(finder->symbol,argv[argptr+1],3) == 0) {
+                    first->type = finder;
+                    break;
+                  }
+                }
+                mol->AddAtom(first);  // add to molecule
+                argptr+=4;
+                break;
+              case 's':
+                j = -1;
+                cout << Verbose(1) << "Scaling all ion positions by factor." << endl;
+                factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor");
+                factor[0] = atof(argv[argptr]);
+                if (argc > argptr+1)
+                  argptr++;
+                factor[1] = atof(argv[argptr]);
+                if (argc > argptr+1)
+                  argptr++;
+                factor[2] = atof(argv[argptr]);
+                mol->Scale(&factor);
+                for (int i=0;i<3;i++) {
+                  j += i+1;
+                  x.x[i] = atof(argv[3+i]);
+                  mol->cell_size[j]*=factor[i];
+                }
+                Free((void **)&factor, "main: *factor");
+                argptr+=1;
+                break;
+              case 'c':
+                j = -1;
+                cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl;
+                // make every coordinate positive
+                mol->CenterEdge((ofstream *)&cout, &x);
+                // update Box of atoms by boundary
+                mol->SetBoxDimension(&x);
+                // translate each coordinate by boundary
+                j=-1;
+                for (int i=0;i<3;i++) {
+                  j += i+1;
+                  x.x[i] = atof(argv[argptr++]);
+                  mol->cell_size[j] += x.x[i]*2.;
+                }
+                mol->Translate((const vector *)&x);
+                break;
+              case 'r':
+                cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl;
+                break;
+              case 'f':
+                int i,j;
+                flag = 0;
+                if (argc > argptr+3) {
+                  cout << Verbose(0) << "Creating connection matrix..." << endl;
+                  start = clock();
+                  mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[argptr++]));
+                  cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl;
+                  cout << Verbose(0) << "What's the desired bond scheme [(B)ottomUp/(T)opDown/(A)NOVA/(C)ombined]: ";
+                  CutCyclic = SaturateBond;
+                  switch (argv[argptr+1][0]) {
+                    default:
+                        Scheme = NoScheme;
+                      break;
+                    case 'B':
+                      CutCyclic = KeepBond;
+                    case 'b':
+                        Scheme = BottomUp;
+                        i = atoi(argv[argptr]);
+                        j = 0;
+                      break;
+                    case 'T':
+                      CutCyclic = KeepBond;
+                    case 't':
+                        Scheme = TopDown;
+                        i = 0;
+                        j = atoi(argv[argptr]);
+                      break;
+                    case 'A':
+                      CutCyclic = KeepBond;
+                    case 'a':
+                        Scheme = ANOVA;
+                        i = atoi(argv[argptr]);
+                        j = 0;
+                      break;
+    //                case 'C':
+    //                  CutCyclic = KeepBond;
+    //                case 'c':
+    //                    if (argc > 5) {
+    //                      i = atoi(argv[4]);
+    //                      j = atoi(argv[6]);
+    //                      Scheme = Combined;
+    //                    }
+    //                    else
+    //                      cerr << "Missing second bond order for TopDown fragmentation in combined approach." << endl;
+    //                  break;
+                  };
+                  if (mol->first->next != mol->last) {
+                    mol->FragmentMolecule((ofstream *)&cout, i, j, Scheme, &configuration, CutCyclic);
+                  }
+                  end = clock();
+                  cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl;
+                  argptr+=2;
+                }
+                break;
+              default:
+                break;
+            }
+          } else {
+            cout << "Cannot apply command line parameter as no valid config file was found." << endl;
+            return 1;
+          }
         }
-        Free((void **)&factor, "main: *factor");
-        break;
-      case 'c':
-        j = -1;
-        cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl;
-        // make every coordinate positive
-        mol->CenterEdge((ofstream *)&cout, &x);
-        // update Box of atoms by boundary
-        mol->SetBoxDimension(&x);
-        // translate each coordinate by boundary
-        j=-1;
-        for (int i=0;i<3;i++) {
-          j += i+1;
-          x.x[i] = atof(argv[3+i]);
-          mol->cell_size[j] += x.x[i]*2.;
-        }
-        mol->Translate((const vector *)&x);
-        break;
-      case 'r':
-        cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl;
-        break;
-      case 'p':
-        cout << Verbose(1) << "Parsing xyz file for new atoms." << endl;
-        if (!mol->AddXYZFile(argv[3]))
-          cout << Verbose(2) << "File not found." << endl;
-        else
-          cout << Verbose(2) << "File found and parsed." << endl;
-        break;
-      case 'f':
-        int i,j;
-        flag = 0;
-        if (argc > 4) {
-          cout << Verbose(0) << "Creating connection matrix..." << endl;
-          start = clock();
-          mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[3]));
-          cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl;
-          cout << Verbose(0) << "What's the desired bond scheme [(B)ottomUp/(T)opDown/(A)NOVA/(C)ombined]: ";
-          CutCyclic = SaturateBond;
-          switch (argv[5][0]) {
-            default:
-                Scheme = NoScheme;
-              break;
-            case 'B':
-              CutCyclic = KeepBond;
-            case 'b':
-                Scheme = BottomUp;
-                i = atoi(argv[4]);
-                j = 0;
-              break;
-            case 'T':
-              CutCyclic = KeepBond;
-            case 't':
-                Scheme = TopDown;
-                i = 0;
-                j = atoi(argv[4]);
-              break;
-            case 'A':
-              CutCyclic = KeepBond;
-            case 'a':
-                Scheme = ANOVA;
-                i = atoi(argv[4]);
-                j = 0;
-              break;
-            case 'C':
-              CutCyclic = KeepBond;
-            case 'c':
-                if (argc > 5) {
-                  i = atoi(argv[4]);
-                  j = atoi(argv[6]);
-                  Scheme = Combined;
-                }
-                else
-                  cerr << "Missing second bond order for TopDown fragmentation in combined approach." << endl;
-              break;
-          };
-          if (mol->first->next != mol->last) {
-            mol->FragmentMolecule((ofstream *)&cout, i, j, Scheme, &configuration, CutCyclic);
-          }
-          end = clock();
-          cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl;
-        }
-        break;                
-      default:
-        break;
-    }
-    if (flag) {
-      mol->CountElements();  // this is a bugfix, atoms should should actually be added correctly to this fragment 
-      mol->CalculateOrbitals(configuration);
-      configuration.InitMaxMinStopStep = configuration.MaxMinStopStep = configuration.MaxPsiDouble;
-      if (argc >= 1)
-        output.open(argv[1], ios::trunc);
-      if (!output)
-        output.open("main_pcp_linux", ios::trunc);
-      if (configuration.Save(&output, periode, mol))
-        cout << Verbose(1) << "Saving of config file successful." << endl;
-      else
-        cout << Verbose(1) << "Saving of config file failed." << endl;
-      output.close();
-      output.clear();
-      // and save to xyz file
-      if (argc >= 1) {
-        strcpy(filename, argv[1]);
-        strcat(filename, ".xyz");
-        output.open(filename, ios::trunc);
-      }
-      if (output == NULL) {
-        strcpy(filename,"main_pcp_linux");
-        strcat(filename, ".xyz");
-        output.open(filename, ios::trunc);
-      } 
-      if (mol->OutputXYZ(&output))
-        cout << Verbose(0) << "Saving of XYZ file successful." << endl;
-      else
-        cout << Verbose(0) << "Saving of XYZ file failed." << endl;
-      output.close();
-      output.clear();
-    }
+  	  } else argptr++;
+  	} while (argptr < (argc-1));
+    if (flag)
+      SaveConfig(ConfigFileName, &configuration, periode, mol);
     delete(mol);
     delete(periode);
@@ -1115,34 +1191,5 @@
       
       case 's': // save to config file
-        // get correct valence orbitals
-        mol->CalculateOrbitals(configuration);
-        configuration.InitMaxMinStopStep = configuration.MaxMinStopStep = configuration.MaxPsiDouble;
-        if (argc >= 1)
-          output.open(argv[1], ios::trunc);
-        if (output == NULL)
-          output.open("main_pcp_linux", ios::trunc);
-        if (configuration.Save(&output, periode, mol))
-          cout << Verbose(0) << "Saving of config file successful." << endl;
-        else
-          cout << Verbose(0) << "Saving of config file failed." << endl;
-        output.close();
-        output.clear();
-        // and save to xyz file
-        if (argc >= 1) {
-          strcpy(filename, argv[1]);
-          strcat(filename, ".xyz");
-          output.open(filename, ios::trunc);
-        }
-        if (output == NULL) {
-          strcpy(filename,"main_pcp_linux");
-          strcat(filename, ".xyz");
-          output.open(filename, ios::trunc);
-        } 
-        if (mol->OutputXYZ(&output))
-          cout << Verbose(0) << "Saving of XYZ file successful." << endl;
-        else
-          cout << Verbose(0) << "Saving of XYZ file failed." << endl;
-        output.close();
-        output.clear();
+        SaveConfig(ConfigFileName, &configuration, periode, mol);
         break;
     };
