#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# 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.

"""chkcrontab - Checks the correctness of crontab files.

Parse crontab files and check each type of line for potential syntax errors.

Basic usage:
  chkcrontab crontab_file
"""

from __future__ import print_function

__author__ = 'lyda@google.com (Kevin Lyda)'

import sys
import chkcrontab_lib as check

from optparse import OptionParser
from argparse import ArgumentParser
try:
  from _version import __version__
except ImportError:
  __version__ = 'unknown'

def main(argv):
  """main program."""
  parser = ArgumentParser(
      description='Parse crontab files; check for potential syntax errors.',
      version=__version__)
  parser.add_argument('crontab', help='crontab file to parse')
  parser.add_argument('-w', '--whitelist', action='append',
      dest='whitelisted_users',
      help='user to ignore when warning of unrecognized users; may be passed multiple times',
      default=['postgres', 'buildbot'])
  parser.add_argument('-n', '--no-check-passwd', action='store_false',
      dest='check_passwd', help='disable user lookup')
  parser.add_argument('-q', '--quiet', action='store_true',
      dest='quiet', help='be quiet')
  args = parser.parse_args()

  if not args.quiet:
    print('Checking correctness of %s' % args.crontab)
  return check.check_crontab(args, check.LogCounter(args.quiet))

if __name__ == '__main__':
  sys.exit(main(sys.argv))
