sub get_options { #process any command line options my $config = shift; #hash ref my ($debug,$test,$ini_file,$max_files, $data_dir,$log_dir,$user,$pass,$verbose, $no_ini); GetOptions('debug=i' => \$debug, 'test=i' => \$test, 'ini_file=s' => \$ini_file, 'max_files=i' => \$max_files, 'data_dir=s' => \$data_dir, 'log_dir=s' => \$log_dir, 'user=s' => \$user, 'pass=s' => \$pass, 'verbose=i' => \$verbose, 'no_ini' => \$no_ini, ); $config->{debug} = $debug if defined $debug; $config->{test} = $test if defined $test; $config->{ini_file} = $ini_file if defined $ini_file; $config->{max_files} = $max_files if defined $max_files; $config->{data_dir} = $data_dir if defined $data_dir; $config->{log_dir} = $log_dir if defined $log_dir; $config->{user} = $user if defined $user; $config->{password} = $pass if defined $pass; $config->{verbose} = $verbose if defined $verbose; $config->{no_ini} = $no_ini if defined $no_ini; } sub init_config { #initialize the config parameters for the program my $config = shift; #hash ref my $bindir = $FindBin::Bin; #these get set no matter what $config->{version} = $VERSION; #these first two are just program constants $config->{revision} = ($REVISION =~ /(\d+\.\d+)/) ? $1 : '?'; #if command line option -no_ini specified, then don't try to load the ini file unless ($config->{no_ini}) { #process the ini file #if we already have an ini_file, use that one, otherwise use the default file my $config_file = $config{ini_file} || "$bindir/configfile.ini"; #default name for ini file my $ini = new ConfigIni($config_file) || croak "could not load ini file $config_file"; $config->{ini_file} = $config_file if not defined $config->{ini_file}; #now load and set the parameters $config->{debug} = $ini->get_ini_val('Debug','Debug',0) if not defined $config->{debug}; $config->{test} = $ini->get_ini_val('Debug','Test',0) if not defined $config->{test}; $config->{max_files} = $ini->get_ini_val('Debug','Max_files',0) if not defined $config->{max_files}; $config->{data_dir} = $ini->get_ini_val('General','Data_dir') if not defined $config->{data_dir}; $config->{log_dir} = $ini->get_ini_val('General','Log_dir') if not defined $config->{log_dir}; $config->{user} = $ini->get_ini_val('General','User') if not defined $config->{user}; $config->{password} = $ini->get_ini_val('General','Password') if not defined $config->{password}; $config->{verbose} = $ini->get_ini_val('General','Verbose',1) if not defined $config->{verbose}; } #unless ($config->{no_ini}) #if log_dir isn't set, put logs in the same directory as the program $config->{log_dir} ||= $bindir; $config->{log_file} = $config{log_dir} . "/log_file_" . strftime("%Y%m%d_%H%M%S_",localtime) . "$$.log"; #init anything that needs to be and might not have been done on either command line or ini_file $config->{debug} = 0 if not defined $config->{debug}; $config->{test} = 0 if not defined $config->{test}; $config->{verbose} = 1 if not defined $config->{verbose}; $config->{max_files} = 0 if not defined $config->{max_files}; #check to make sure info is complete croak "User must be specified in [General] section of ini file" if !defined $config->{user}; croak "Password must be specified in [General] ini file" if !defined $config->{password}; croak "Data_dir must be specified in [General] section of ini file" if !defined $config->{data_dir}; } #sub init_config