#!/bin/sh

# This script takes a raw Windows event log and adds it to a 
# previously built database generated by grokevt-builddb.
# See the man page for more details.
#
# Copyright (C) 2006 Timothy D. Morgan
#
# 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 version 2 of the
# License.
#
# 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.
#
# $Id: grokevt-addlog 79 2006-05-08 22:55:52Z tim $


usage()
{
  echo "USAGE:" 1>&2
  echo "  $0 <DATABASE_DIR> <EVT_FILE> <NEW_TYPE> <BASE_TYPE>" 1>&2
  echo 1>&2
  echo "This script takes a raw Windows event log and adds it to a" 1>&2
  echo "previously built database generated by grokevt-builddb." 1>&2
  echo "See the man page for more details." 1>&2
}

if [ $# -ne 4 ]; then
  usage
  exit 1
fi

DATABASE_DIR="$1"
EVT_FILE="$2"
NEW_TYPE="$3"
BASE_TYPE="$4"


if [ ! -d "$DATABASE_DIR" ]; then
  echo "ERROR: '$DATABASE_DIR' is not a directory." 1>&2
  exit 2
fi

if [ ! -r "$EVT_FILE" ]; then
  echo "ERROR: '$EVT_FILE' could not be read." 1>&2
  exit 2
fi

if [ -e "${DATABASE_DIR}/logs/${NEW_TYPE}.evt" ]; then
  echo "ERROR: A '$NEW_TYPE' log already exists." 1>&2
  exit 2
fi

if [ ! -d "${DATABASE_DIR}/services/${BASE_TYPE}" ]; then
  echo "ERROR: A '$BASE_TYPE' log does not exist." 1>&2
  exit 2
fi


cp "$EVT_FILE" "${DATABASE_DIR}/logs/${NEW_TYPE}.evt"\
  || ( echo "ERROR: Could not copy log file." 1>&2; exit 3 )

ln -s "$BASE_TYPE" "${DATABASE_DIR}/services/${NEW_TYPE}"\
  || ( echo "ERROR: Could not create symlink to BASE_TYPE." 1>&2; exit 3 )
