#!/usr/bin/env superdoit_solo
#
# Read and validate a GemStone stone, gem, or x509 netldi configuration file,  
# to verify there are no syntax or other errors that make it unusable.
# Print details of errors found and success or failure. 
# Returns 0 if the file is valid, 1 if errors found or file does not exist.
#
options
{
SuperDoitRequiredOptionWithRequiredArg long: 'path' short: 'p'.
SuperDoitRequiredOptionWithRequiredArg long: 'type' short: 't'.
}
%
usage
-----
USAGE 
  $basename -p aString -t anInt 
  $basename --path=aString --type=anInt  
  $basename -h | --help  

DESCRIPTION
  validate the configuration file at the location aString with the type <int> and print the result of validation.

OPTIONS
  -p, --path <aString>    <aString> is a absolute or relative path to a configuration file. 
  -t, --type <anInt>      <anInt> is the intended type to parse the config file: 1 for the stone, 2 for gem, 
                             3 for an X509 netldi config file.
  -h, --help              Display usage message and exit.

EXAMPLES
  $basename --help
  $basename -p $GEMSTONE/data/system.conf -t 1
  $basename --path=$GEMSTONE/data/system.conf --type=1 
-----
%
doit
| result aPath expPath aType |
aType :=  Integer fromString: self type.
aPath := self path.

[ expPath := GsFile _expandFilename: aPath  isClient: false .
  expPath ifNil:[ Error signal:'Unable to expand environment variable(s) in ', aPath printString ].
  expPath = aPath ifFalse: [
        GsFile gciLogServer:'After expanding environment variables, path = ', expPath printString.
         ].
  (GsFile existsOnServer: expPath) == true ifFalse: [
     Error new _number: 2052; reason: #REP_ERR_CANT_OPEN_FILE; details: 'File ', expPath, ' does not exist' ; signal
     ].
  System parseConfigFile: expPath processType: aType  .
   result := true .
   ] on: Error do: [:ex | 
       result := ex. ex return 
       ].
result == true 
  ifTrue: [ GsFile gciLogServer: 'Validation succeeded.'. self noResult]
  ifFalse:[ GsFile gciLogServer: 'VALIDATION FAILED!'. result signal ].
%

