#!/usr/bin/perl
#
# $Id$
#
# (C) ITEG IT-Engineers GmbH 2015
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

use strict;

if ( -f "src/lib/cft-functions.pl" ) {
  require "src/lib/cft-functions.pl";
} else {
  require "/usr/lib/config-file-tools/cft-functions.pl";
}

$main::scriptName = "cft-setoptions";

$main::helpMessage = '
 '.$main::scriptName.' sets options in one or more config files.

 Syntax:
  '.$main::scriptName.' [Options] InputOptions file ...

 General options:

  --help     Prints command syntax.
  --version  Prints command version info and exits.

  -d         Debug mode, logs everything.
  -q         Quiet mode, supresses informative messages, for  feeding.
  -v         Verbose verboser, logs more.

 Input options:

  -i file    Uses specied file as source of config options to set.
             Can be repeated to use multiple files.
  -s n=v     Set option n to value v.

 Target options:

  file ...    Config file(s) to work on.

  -a          Add options to set that are not yet there.
  -n          Add new options only, do not alter existing options.
  -u          Update, only existing options are changed. Default.
 
  -c comment  Over new value insert line with \'# comment\'.
  -k          Keep former value as comment, i.e. prefixed with \'#\'.
  -t          Over new value insert line with \'# by cft-xy at timestamp\'.

  -f format   Specify format. For now only \'n=v\' format is supported.
              Ideas for other formats include \'ini\', \'n_v\'.

';

initCFTFunctions();

foreach my $targetFile (sort(@main::targetFiles)) {
  if ($main::verbosity gt 2) {
    print "Processing ".$targetFile."\n";
  }
  updateConfFile($targetFile); 
}

if ($main::verbosity gt 0) {
  print $main::nrFilesChanged." of ".$main::nrTargetFiles." conf files changed.\n";
}

cleanup();

