#!/bin/sh

PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/nn/sbin:/nn/bin:/opt/bin:/opt/sbin; export PATH

refresh_list=0
root_dir=/tmp/RD
debug_level=0

log() {
  if [ ! `expr $debug_level \> $1` = 0 ] ; then
    shift
    echo "$*"
  fi
}

make_root_dir() {
  if [ ! -d $root_dir ] ; then
    mkdir -p $root_dir
  fi
}

check_file_signature() {
  tgt_file=$1.sig
  if [ "$2"b = exactb ] ; then
    tgt_file=$1
  fi
  cnt=`wc -l $tgt_file | cut -f1 -d ' '`
  if [ `expr $cnt == 0` = 1 ] ; then
    return 0
  fi
  md5sum -c $tgt_file
  return $?
}

set_file_signature() {
  output_file=$3
  if [ "$3"b = b ] ; then
    output_file=$1.sig
  fi
  if [ "$2"b = appendb ] ; then
    md5sum $1 >> $output_file
  else 
    md5sum $1 > $output_file
  fi
  return $?
}

make_root_dir

rpm_list=`rpm -qa`

echo $rpm_list > $root_dir/working_list
diff $root_dir/working_list $root_dir/current_list
if [ ! "$?" = "0" ] ; then
  log 1 rmp list is different then cached list.. forced rebuild
  refresh_list=1
fi

is_config_file() {
  config_list=$1
  tgt_file=$2
  for i in `echo -n $config_list | tr ' ' '\n' | grep $2` ; do 
    if [ "$2" = "$i" ] ; then
      echo -n 1
      return
    fi
  done
  echo -n 0
}

parse_file () {
  echo > /dev/null
}

generate_other_list() {
  other_dir=$root_dir/$1
  if [ ! -d $other_dir ] ; then
    mkdir -p $other_dir
  fi
  other_list=$root_dir/$1/other_list
  check_file_signature $other_list
  if [ "$?" = "0" ] ; then
    return 0
  fi
  echo `rpm -q --list $1` > $root_dir/file_list
  echo `rpm -qc $1` > $root_dir/config_list
  echo -n > $other_list
  config_list=`cat $root_dir/config_list | tr ' ' '\n'`
  rm -f $root_dir/$1/signatures
  touch $root_dir/$1/signatures
  for j in `cat $root_dir/file_list | tr ' ' '\n'` ; do
    if [ ! -f $j ] ; then
      continue
    fi
   
    result=`is_config_file "$config_list" "$j"`
    if [ "$result" = "0" ] ; then
       echo "$j" >> $other_list
       set_file_signature "$j" append $root_dir/$1/signatures    
    fi
  done
  set_file_signature $other_list
  set_file_signature $root_dir/$1/signatures append $other_list.sig
}

check_signatures() {
  tgt_dir=$root_dir/$1
  if [ ! -f $tgt_dir/other_list.sig ]  ;  then
    log 0 missing signatures of files and overall signatures
    return 1
  fi
  check_file_signature $tgt_dir/other_list.sig  exact
  if [ ! "$?" = 0  ] ; then
    log 0 failed overall signature check
    return 1
  fi
  
  if [ ! -f $tgt_dir/signatures ] ; then
    log 0 missing individual file signatures
    return 1
  fi
  check_file_signature $tgt_dir/signatures exact
  if [ ! "$?" = 0 ] ; then
    log 0 failed file signature check
  fi
  return 0
}

if [ $refresh_list = 1 ] ; then
  log 1 must recreate rpm list
  rm -Rf $root_dir
  make_root_dir
  echo $rpm_list > $root_dir/current_list
  set_file_signature $root_dir/current_list
  for i in $rpm_list ; do 
    generate_other_list $i
  done
  for i in $root_dir/*/signatures ; do 
    set_file_signature $i append $root_dir/current_list.sig
  done
  exit 0
fi

for i in $rpm_list ; do 
  check_signatures  $i
  if [ ! $? = 0 ] ; then 
    exit 1
  fi
done
exit 0
