#!/bin/bash
usage ()
{
    prog=`basename $0`
    echo  "Usage: $prog [-d <ELDKROOT>] [-a arch] [-h] [-q]"
    exit 1
}

checkfiles()
{
    while read
      do
      check_file $REPLY
      if [ $? != 0 ]; then
	  return 1
      fi
    done
    return 0
}

check_file()
{
    local file=`echo $1 | sed -e "s/'//g"`
    local ownership=$2
    local mode=`echo $3 | cut -b 4-`
    local sbit=`echo $mode | cut -b 1`
    local uid
    local gid
    local realuid
    local realgid

    if [ $sbit = 0 ]; then
	return 0
    else
#debug	echo "$file $ownership $mode"
	if [ ! -f $file ]; then
	    echo "file $file does not exist?"
	    return 1
	fi
	uid=`echo $ownership | awk -F : '{ print $1 }'`
	realuid=`cat $ELDKROOT/$TARGET/etc/passwd | \
	    grep "^$uid" | awk -F : '{print $3}'`
	if [ -e $realuid ]; then
	    echo "Uid \"$uid\" not found in $ELDKROOT/$TARGET/etc/passwd"
	    echo "Will use uid 0..."
	    realuid=0
	fi
	gid=`echo $ownership | awk -F : '{ print $2 }'`
	realgid=`cat $ELDKROOT/$TARGET/etc/group | \
	    grep "^$gid" | awk -F : '{print $3}'`

	if [ -e $realgid ]; then
	    echo "Gid \"$gid\" not found in $ELDKROOT/$TARGET/etc/group"
	    echo "Will use gid 0..."
	    realgid=0
	fi
	if [ $verbose = 1 ]; then
	    echo "chown $realuid:$realgid $file"
	fi
	chown $realuid:$realgid $file
	if [ $? != 0 ]; then
	    return 1
	fi
	if [ $verbose = 1 ]; then
	    echo "chmod $mode $file"
	fi
	chmod $mode $file
	if [ $? != 0 ]; then
	    return 1
	fi
    fi
    return 0
}

verbose=1
while getopts "a:d:qh" opt
do
  case "$opt" in
      h)      usage ;;
      a)      arch="$OPTARG" ;;
      d)      ELDKROOT="$OPTARG" ;;
      q)      verbose=0 ;;
      *)      echo "$0: Invalid option '$opt'" >&2 ; usage ;;
  esac
done

if [ $# != 0 -a $OPTIND = 1 ]; then
  usage
fi

shift `expr $OPTIND - 1`

if [ `id -u` -ne 0 ]; then
 echo "Should be executed as root!"
 exit 1
fi

unset CROSS_COMPILE
if [ -z $ELDKROOT ]; then
    ELDKROOT=`pwd`
fi

# Sanity check
if [ $ELDKROOT = "/" ]; then
    echo "Will not work with ELDKROOT set to system root"
    exit 1
fi

# Get the list of targets installed

if [ ! -f $ELDKROOT/version ]; then
    echo "No \"version\" file at \"$ELDKROOT\"."
    exit 1
fi

if [ -z $arch ]; then
    targetlist=`cat $ELDKROOT/version | grep -v ELDK | awk -F : '{print $1}'`
else
    targetlist=$arch
fi

trap "exit 1" 1 2 3 15
for t in $targetlist
do
  TARGET=$t
  if [ ! -d $ELDKROOT/$t ]; then
      echo "No dir $ELDKROOT/$t ?"
      continue
  fi

  if [ $t = "arm" -o $t = "armVFP" -o $t = "ppc64" ]; then
    t="$t-linux"
  fi

  pkglist=`$ELDKROOT/bin/$t-rpm -qa`
  for p in $pkglist
  do

# A little speed up - skip some packages...
    echo $p | egrep -qs 'kernel-source|kernel-headers|u-boot'
    if [ $? = 0 ]; then
	if [ $verbose = 1 ]; then
	    echo "Skipping $p..."
	fi
	continue;
    fi

    if [ $verbose = 1 ]; then
	echo "Checking package $p..."
    fi
    $ELDKROOT/bin/$t-rpm -q --queryformat="[%{FILENAMES:shescape} \
	%{FILEUSERNAME}:%{FILEGROUPNAME} \
	%7.7{FILEMODES:octal}\n]" $p | checkfiles
    if [ $? != 0 ]; then
	exit 1
    fi
  done
done
