#!/usr/bin/perl
#
# $Id: audit 5147 2005-08-26 02:43:09Z jesse $
#
# ----------------------------------------------------------------------
#    PROPRIETARY DATA of IMMUNIX INC.
#    Copyright (c) 2004, IMMUNIX (All rights reserved)
#
#    This document contains trade secret data which is the property
#    of IMMUNIX Inc.  This document is submitted to recipient in
#    confidence. Information contained herein may not be used, copied
#    or disclosed in whole or in part except as permitted by written
#    agreement signed by an officer of IMMUNIX, Inc.
# ----------------------------------------------------------------------

use strict;
use FindBin;
use Getopt::Long;

use Immunix::SubDomain;

use Data::Dumper;

use Locale::gettext;
use POSIX;

# initialize the local poo
setlocale(LC_MESSAGES, "");
textdomain("subdomain-utils");

$UI_Mode = "text";

# options variables
my $help           = '';
  
GetOptions(
  'dir|d=s'     => \$profiledir,
  'help|h'      => \$help,
);
  
# tell 'em how to use it...
&usage && exit if $help;

# let's convert it to full path...
$profiledir = get_full_path($profiledir);
  
unless(-d $profiledir) {
  UI_Important("Can't find subdomain profiles in $profiledir.");
  exit 1;
}

# read the settings in /etc/logprof.conf
readconfig();

# what are we profiling?
my @profiling = @ARGV;

unless(@profiling) {
  @profiling = ( UI_GetString("Please enter the program to switch to audit mode: ", "") );
}

for my $profiling (@profiling) {

  next unless $profiling;

  my $fqdbin;
  if(-e $profiling) {
    $fqdbin = get_full_path($profiling);
    chomp($fqdbin);
  } else {
    if($profiling !~ /\//) {
      my $which = which($profiling);
      if($which) {
        $fqdbin = get_full_path($which);
      }
    }
  }

  if(-e $fqdbin) {

    my $filename;
    if($fqdbin =~ /^$profiledir\//) {
      $filename = $fqdbin;
    } else {
      $filename = getprofilefilename($fqdbin);
    }

    # argh, skip directories
    next unless -f $filename;

    # skip rpm backup files
    next if $filename =~ /\.rpm(save|new)$/;

    printf(gettext('Setting %s to audit mode.'), $fqdbin);
    print "\n";
    setprofileflags($filename, "audit");

    system("cat $filename | $parser -I$profiledir -r >/dev/null 2>&1") if check_for_subdomain();
  } else {
    if($profiling =~ /^[^\/]+$/) {
       UI_Info(sprintf(gettext('Can\'t find %s in the system path list.  If the name of the application is correct, please run \'which %s\' as a user with the correct PATH environment set up in order to find the fully-qualified path.'), $profiling, $profiling));
      exit 1;
    } else {
      UI_Info(sprintf(gettext('%s does not exist, please double-check the path.'). $profiling));
      exit 1;
    }
  }
}

exit 0;

sub usage {
  UI_Info(sprintf(gettext("usage: \%s [ -d /path/to/profiles ] [ program to switch to audit mode ]"), $0));
  exit 0;
}

