#!/usr/bin/env ruby
#encoding: UTF-8 
=begin
/***************************************************************************
 *   ©2019-2019 Michael Uplawski <michael.uplawski@uplawski.eu>            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
=end

require_relative '../lib/spreadsheet_manip'

# Delete spreadsheet lines by value, i.e. if the cell in a named column
# contains a given value.
#
# OOXML only.

def call_for_help
  puts (Translating::trl("Call with option -h or --help to see the option summary.") )
  exit false
end

# Prepare a class-level logger.
# This is the Object-class and messages will be prefixed with 'Top Level'.
self.extend(Logging)
log = init_logger(STDOUT, Logger::DEBUG)

# allow specific reactions to the user-provided parameters. 
evaluate_delete = lambda do | expr|
  delref = expr.split('=')
  if(delref.length == 2)
    return delref
  else
    pe = OptionParser::ParseError.new
    pe.reason = 'The option -d (or --delete) needs a value in the form column=value'
    raise pe
  end
end

if ARGV.empty?
  call_for_help
  exit false
end

# read from the user-input all options which are not file-names. Handle the delete-option
# as needed.
options = ArgParser.parse(ARGV, :delete => evaluate_delete);
if(options.delete && options.delete.respond_to?(:to_ary) && options.delete.length == 2)
  # Then, look what's left
  if ARGV.empty? 
    log.error(Translating::trl('No spreadsheet file.') )
    call_for_help
  elsif ARGV.length > 1
    log.error( Translating::trl('Too many trailing arguments. Only a spreadsheet file is expected.'))
    # bail out
    call_for_help
  else 
    # Remember the current working-directory, for later. This is probably
    # superfluous.
    cur_dir = Dir.pwd
    file = ARGV[0]
    # Do some checks.
    msg = File_Checking::file_check(file, :exist, :file, :readable, :writable)
    # Do more checks. 
    msg ||= File_Checking::mime_check(file, 'application/zip')
    msg ||= File_Checking::magic_check(file, 'Microsoft OOXML')
    if msg
      # Allow the translation of error-messages.
      msg = msg.delete_prefix(file).strip
      # ... there
      log.error(file.dup << ' ' << Translating::trl(msg))
      call_for_help
    end
    document = Spreadsheet.instance()
    if(options.sheet ) 
      document.options = options
      document.file=ARGV[0]
      sheet = document.sheet(options.sheet)
      log.debug 'sheet is ' << sheet.name
      # delete the lines as demanded. If possible.
      sheet.remove_line_bv(options.delete.first, options.delete.last)
      document.update_sheet(sheet)
    else
      @log.error(trl('Missing sheet. Aborting.') )
      exit false
    end
    # for completeness... may be pointless, though.
    Dir.chdir(cur_dir)
    document.cleanup
  end
else
  @log.error(trl('Cannot delete lines as demanded') << ' (' << options.delete.join("=") << ').')
  @log.error (trl("The option -d (or --delete) needs a value in the form column=value") << '.')
end

#EOF
