#!/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 expression, i.e. if the cell in a named column
# conforms to a condition.
#
# OOXML only.
# Prepare a class-level logger.
# This is the Object-class and messages will be prefixed with 'Top Level'.
self.extend(Logging)
self.extend(Translating)
log = init_logger(STDOUT, Logger::DEBUG)

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

# separate the cell-reference from the expression.
def split_expression(del_option)
  if(del_option && del_option.respond_to?(:to_str) )
    ar = del_option.split
    cell_reference = ar.shift
    if(cell_reference.match(/[[:alpha:]]+/) )
       expression = ar.join(' ')
       return cell_reference, expression
    end
  end
  @log.error(trl('Cannot delete lines as demanded') << ' (' << del_option.join(' ') << ').')
  @log.error(trl("The option -d (or --delete) needs a value in the form 'column expression'") << '.')
  @log.info(trl('Known expressions are') << ' (november 2019)' << ': ' << Sheet::Known_Expressions.join(', ') )
  exit false
end

# allow specific reactions to the user-provided parameters, like here: none
# needed (see other executable).
evaluate_delete = lambda do |expr|
   return expr
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 ARGV.empty? 
  # Then, look what's left
  log.error(trl('No spreadsheet file.') )
  call_for_help
elsif ARGV.length > 1
  log.error( trl('Too many trailing arguments. Only a spreadsheet file is expected.'))
  # bail out
  call_for_help
else 
  # get the expression to evaluate (reference, expession)
  expr = split_expression(options.delete)
  # 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 << ' ' << 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_be(expr.first, expr.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 # end if ARGV is okay.

#EOF
