Description: source: format usages and helps
 Attempt to format the help message of the evolver front-end program
 wrt UN*X customs in view to employ help2man(1) to generate manpages.
 The help option entries have been updated wrt the evolver.1 manpage.
 A version option `-V' has been introduced, this option displays the
 Surface Evolver version (first line), the copyright (second line), and
 the actual front-end banner which shows useful compilation information
 (second paragraph). Both the help option `-h' and the version option `-V'
 exit after displaying. Meant to be submitted to the upstream maintainer.
Origin: debian
Forwarded: by email
Author: Jerome Benoit <calculus@rezozer.net>
Last-Update: 2016-07-28

--- a/src/tmain.c
+++ b/src/tmain.c
@@ -101,7 +101,9 @@
 * purpose: program entry point
 */
 int main(int argc,char *argv[]) 
-{ int pause_flag=0;  /* whether to pause after banner message */
+{ int banner_exit_flag=0;   /* whether to exit after preamble message */
+	int pause_flag=0;  /* whether to pause after banner message */
+	char evolver_invocation_short_name[31]="evolver-SUFFIX-EXTRASUFFIX_EXE";
 
   msgmax = 2000; 
   if ( !msg ) msg = my_list_calloc(1,msgmax,ETERNAL_BLOCK); 
@@ -109,7 +111,80 @@
 
   outfd = stdout;
   erroutfd = stderr;
-  sprintf(msg,"Surface Evolver %s, %d-bit\n",VERSION,8*(int)sizeof(int*));
+
+	{	char *dum = strrchr(argv[0],PATHCHAR);
+		if (dum == NULL) dum = argv[0];
+		else if (*dum == PATHCHAR) ++dum;
+		if (!(strncmp(dum,"evolver",7)))
+		{ snprintf(evolver_invocation_short_name,31,"%s",dum);
+#if defined(WIN32) || defined(_WIN64) /* guessed code foe Windows systms */
+			dum = strrchr(evolver_invocation_short_name,'.');
+			if ((dum != NULL) && (!(strncmp(dum,".exe",4)))) *dum = '\0';
+#endif
+		}
+		else
+		{	*(evolver_invocation_short_name+7) = '\0'; }
+	}
+
+  /* preparse command line options */
+#if defined(SGI_MULTI) || defined(THREADS)
+#define HELP_USAGE_MULTIPROCESS " [-pN]"
+#else
+#define HELP_USAGE_MULTIPROCESS ""
+#endif
+	if ( argc > 0 )
+	{	int pargc = argc;
+		char **pargv = argv;
+		pargv++; pargc--;
+		while (  pargc && (pargv[0] != NULL) && (pargv[0][0] == '-') )
+		{	switch ( pargv[0][1] )
+			{	default : break;
+				case 'h' :
+						sprintf(msg, "Usage: %s [-a][-d][-e][-i][-m][-q][-w][-x][-y][-h][-V] [-f FILE]" HELP_USAGE_MULTIPROCESS " [DATAFILE]\n",
+							evolver_invocation_short_name); outstring(msg);
+						outstring("Options:\n");
+						outstring("  -a       Autoconvert to named quantities when needed (default is on);\n");
+						outstring("           use `-a-' to deactivate autoconversion.\n");
+						outstring("  -d       Begin with parser debugging on (equivalent to `debug' runtime command);\n");
+						outstring("           beware of copious output.\n");
+						outstring("  -e       Echo stdin to stdout; meant for testing piped input.\n");
+						outstring("  -f FILE  After loading DATAFILE, read commands from file, then command line prompt.\n");
+						outstring("  -i       Preserve DATAFILE numbers for element id's, rather than renumbering.\n");
+						outstring("  -m       Begin with memory debugging on (equivalent to `memdebug' runtime command);\n");
+						outstring("           beware of copious output.\n");
+#if defined(SGI_MULTI) || defined(THREADS)
+						outstring("  -pN      Run with N concurrent processes.\n");
+#endif
+						outstring("  -q       Convert to named quantities at start (equivalent to `convert_to_quantities'\n");
+						outstring("           runtime command).\n");
+						outstring("  -w       Exit immediately after any warning or error message; meant for batch runs.\n");
+						outstring("  -x       Exit immediately after any error message; meant for batch runs.\n");
+						outstring("  -y       Break to user prompt after any warning message.\n");
+						outstring("  -h       Display this help, then exit.\n");
+						outstring("  -V       Display the version and the banner, then exit.\n");
+						outstring("\n");
+						outstring("Report bugs to Ken Brakke <brakke@susqu.edu>\n");
+						my_exit(0);
+					break;
+				case 'V' :
+						sprintf(msg,
+								"%s - Surface Evolver %s"
+								"\n",
+							evolver_invocation_short_name,
+							evolver_version
+							); outstring(msg);
+						outstring("Copyright (C) 1989-2016 Ken Brakke <brakke@susqu.edu>\n");
+						outstring("\n");
+						outstring("*Banner*\n");
+						banner_exit_flag = 1;
+					break;
+			}
+		pargv++; pargc--;
+		}
+	}
+#undef HELP_USAGE_MULTIPROCESS
+
+  sprintf(msg,"Surface Evolver %s, %d-bit.\n",VERSION,8*(int)sizeof(int*));
   outstring(msg);
 #ifdef LONG_ID
   if ( sizeof(int*) == 8 )
@@ -135,15 +210,22 @@
 #elif defined(LONGDOUBLE)
   sprintf(msg,"Compiled for %d-byte long double, %d digits precision.\n",(int)sizeof(REAL),DPREC);
   outstring(msg);
+#elif defined(FLOAT)
+#else
+  sprintf(msg,"Compiled for double, %d digits precision.\n",DPREC);
+  outstring(msg);
 #endif
-  outstring("\n");
 
 #ifdef MPI_EVOLVER
   MPI_Barrier(MPI_COMM_WORLD); /* wait for everybody to print */
 #endif
 
+	if ( banner_exit_flag )
+	{	my_exit(0); }
 
- if ( sizeof(element_id) > sizeof(REAL) )
+  outstring("\n");
+
+	if ( sizeof(element_id) > sizeof(REAL) )
     kb_error(3102,"Bad datatype sizes: element_id is %d bytes, but REAL is %d.\n",
       UNRECOVERABLE);
       
@@ -193,13 +275,12 @@
           FreeConsole();
 #endif
             break;
-          case 'E': err_tok_gen_flag = 1;
-                    break;
+          case 'E': err_tok_gen_flag = 1; break;
           case 'a': auto_convert_flag = (argv[0][2]=='-') ? 0 : 1; break;
           case 'q': option_q = (argv[0][2]=='-') ? 0 : 1; break; 
           case 'Q': quiet_load_flag = 1; break;
           case 'e': echo_flag = 1; break;
-		  case 'Z': pause_flag = 1; break;  /* chance to attach debugger */
+          case 'Z': pause_flag = 1; break;  /* chance to attach debugger */
 #ifdef MPI_EVOLVER
           case 'z': mpi_debug = 1; break;
 #endif
@@ -251,17 +332,13 @@
                 }
 #if defined(SGI_MULTI) || defined (THREADS)
                 if ( procs_requested < 1 )
-                  { kb_error(1321, 
-                      "-p with nonpositive number. Threads set to 1.\n",
-                                    WARNING);
+                  { kb_error(1321,"-p with nonpositive number. Threads set to 1.\n",WARNING);
                      procs_requested = 1;
                   }
                 if ( procs_requested > MAXPROCS )
-                { sprintf(errmsg,
-    "This Evolver only compiled for a maximum of %d threads.\n",MAXPROCS);
+                { sprintf(errmsg,"This Evolver only compiled for a maximum of %d threads.\n",MAXPROCS);
                    kb_error(2551,errmsg,WARNING);
-                  sprintf(errmsg,
-     "Threads set to %d. Recompile with -DMAXPROCS=%d if you want more.\n",
+                  sprintf(errmsg,"Threads set to %d. Recompile with -DMAXPROCS=%d if you want more.\n",
                     MAXPROCS,procs_requested);
                   erroutstring(errmsg);
                   procs_requested = MAXPROCS;
@@ -273,41 +350,28 @@
 #else
                 kb_error(1322,"-p option not effective.  This Evolver not compiled for multithreading.\n", WARNING);
 #endif
-                break; 
+                break;
           case 'A' : cpu_affinity_flag = 1; break;
           case 'x' : exit_after_error = 1; break;
           case 'w' : exit_after_warning = exit_after_error = 1; break;
           case 'y' : break_after_warning = 1; break;
           case 'm' : memdebug = 1; break;
           case 'l' : just_lex_flag = 1; break;
-          default:
-                 sprintf(msg,"Illegal option: %s\n",argv[0]); outstring(msg);
-          case 'h' :
-                 outstring("Legal options: \n"); 
-                 outstring("  -ffilename          take commands from file\n"); 
-                 outstring("  -i                     use id numbers as in datafile\n"); 
-                 outstring("  -q                     convert to named quantities\n"); 
-                 outstring("  -a                     auto convert to named quantities when needed\n"); 
-                 outstring("  -x                     exit after error\n"); 
-                 outstring("  -w                     exit after warning\n"); 
-                 outstring("  -y                     break after warning\n"); 
-                 outstring("  -d                     parser debugging on\n"); 
-                 outstring("  -m                     memory debugging on\n"); 
-#ifdef SGI_MULTI
-                 outstring("  -pn                    use n processes \n"); 
-#endif
-#if defined(THREADS)
-                 outstring("  -pn                    use n worker threads \n"); 
-#endif
-                 outstring("  -h                     print this help\n"); 
-                 break;
+          case 'h' : /* preparse case */ break;
+					case 'V' : /* preparse case */ break;
+          default :
+							sprintf(msg,"Illegal option: %s\n",argv[0]); outstring(msg);
+							outstring("\n");
+							sprintf(msg,"Try '%s -h' for more information\n",evolver_invocation_short_name); outstring(msg);
+							my_exit(1);
+							break;
             }
         argv++; argc--;
      }
   }
 
   if ( pause_flag )
-  { 
+  {
     #ifdef MPI_EVOLVER
       MPI_Barrier(MPI_COMM_WORLD);   // to get all banners printed first
       if ( this_task == MASTER_TASK )  // only master gets input, but all tasks wait on master anyway.
