#!/usr/local/bin/ruby

### palm2mhc -- copy articles from palm to new mhc repository.
##
## Author:  Yoshinari Nomura <nom@mew.org>
##
## Created: 1999/10/08
## Revised: 1999/10/22 09:40:15
##

$DEBUG = false

require 'kconv'
require 'mhc-palm'
require 'mhc-date'
require 'mhc-schedule'

def usage
  print '
palm2mhc -- Add palm articles to a mhc repository.

  usage: palm2mhc [-a | -u] [-n] [-d dev] [-r dir]

    -a     : Add all articles of a palm to a mhc repository.
    -u     : Add dirty articles of a palm to a mhc repository.
    -n     : Do nothing effectives to mhc. Useful for checking.
    -d dev : Set the device file connected to the palm.
             default value is /dev/pilot
    -r dir : Set repository directory of mhc.
             if not exists, palm2mhc makes the directory.
             default value is /tmp/mhc-schedule
'
  exit 1
end

##
## option check.
##

$flag_update  = false
$flag_append  = false
$flag_noharm  = false
$flag_dir     = '/tmp/mhc-schedule'
$flag_device  = '/dev/pilot'

while ARGV .length > 0
  case ARGV[0]
  when '-n'
    $flag_noharm  = true
  when '-u'
    $flag_update  = true
  when '-a'
    $flag_append  = true
  when '-d'
    ARGV .shift
    $flag_device = ARGV[0] 
  when '-r'
    ARGV .shift
    $flag_dir = ARGV[0]
  else
    usage()
  end
  ARGV .shift
end

if !File .exist?($flag_device)
  STDERR .print "Can not open #{$flag_device}.\n"
  exit 1
end

usage() if !($flag_update || $flag_append) || ($flag_update && $flag_append)

##
## Open palm & copy to new mhc repository.
##

psock = Pilot .new($flag_device)
STDERR .print "Press Sync Button\n"

if psock .listen
  pdb = PilotApptDB .new(psock, "DatebookDB")
else
  STDERR .print "Can not open #{$flag_device}.\n"
  exit 1
end

mdb = MhcScheduleDB .new($flag_dir)

pdb .each_record{|rec|
  if $flag_append || ($flag_update && rec .attribute_dirty?)
    x = rec .to_xsc

    print "****************************************************************\n"
    print x .dump
    print "****************************************************************\n"

    if !$flag_noharm
      mdb .add_sch(x)
    end
  end
}

##
## close palm & exit
##

if !$flag_noharm
  pdb .reset_sync_flags  ## remove all dirty flag in palm.
end

pdb .close
psock .close
exit 0

### Copyright Notice:

## Copyright (C) 1999 Yoshinari Nomura.
## All rights reserved.

## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
## 3. Neither the name of the team nor the names of its contributors
##    may be used to endorse or promote products derived from this software
##    without specific prior written permission.
## 
## THIS SOFTWARE IS PROVIDED BY Yoshinari Nomura AND CONTRIBUTORS ``AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
## FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
## Yoshinari Nomura OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
## INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
## OF THE POSSIBILITY OF SUCH DAMAGE.

### palm2mhc ends here
