#!/bin/bash
#
# Copyright (C) 2008 Novell Inc.
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street,
# Fifth Floor,
# Boston, MA  02110-1301,
# USA.
#
# $Id: create_sha1um,v 1.8 2008/06/25 12:18:19 lrupp Exp lrupp $
#

function usage (){
    echo
    echo "Usage: `basename $0` [--meta] <targetdir>"
    echo "       --meta : also create MD5SUMS.meta file"
    echo "       --quiet: don't output anything"
    echo 
    echo "       Creates SHA1SUMS files in each subdirectory."
    echo "       Sometimes useful for debugging inst-source problems ;-)"
    echo
    exit $1
}

function output(){
	if [ "$QUIET" != "yes" ]; then
		echo "$1"
	fi
}

if [ -z "$1" ]; then
  usage 1;
fi
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] ; then
  usage 0;
fi

unset LANGUAGE
unset LANG
export LC_ALL=POSIX
umask 022

CREATE_META=
QUIET="no"

if test "x$1" = "x--meta"; then
    CREATE_META=1
    shift
fi
if test "x$1" = "x--quiet"; then
	QUIET="yes"
	shift
fi

ROOTDIRS="$*"
TMPFILE=$(mktemp /tmp/.create_sha1sums.XXXXXX)
MYPWD=$(pwd)

test -z "$ROOTDIRS" && ROOTDIRS="$MYPWD"

for ROOTDIR in "$ROOTDIRS" ; do
    case "$ROOTDIR" in
	/*) ;;
	*) ROOTDIR="$MYPWD/$ROOTDIR" ;;
    esac

    test -d "$ROOTDIR" || {
        echo "WARNING: $ROOTDIR is not a directory.  Skipping it." >&2
    }

    for DIR in $(find "$ROOTDIR" -type d) ; do
        cd "$DIR"
        FILES=$(
          for FILE in * ; do
            test -f "$FILE" || continue
            test -L "$FILE" && continue
            test -n "$SHA1SUM_SKIPFILES" && {
                IS_SKIP=false
		        set -f
                for i in $SHA1SUM_SKIPFILES ; do
		          set +f
		          case "$FILE" in
			        ($i) IS_SKIP=true
			        break
			        ;;
		          esac
                done
		        set +f
                test $IS_SKIP = true && continue
            }
            case "$FILE" in
              (*.swp|SHA1SUMS*)
                continue
                ;;
              (*)
                echo $FILE
                ;;
            esac
          done)
        if test -z "$FILES" ; then
            rm -f SHA1SUMS*
        else
	        echo "$FILES" | xargs sha1sum > $TMPFILE
            test -e SHA1SUMS || touch SHA1SUMS
	        cmp -s $TMPFILE SHA1SUMS || {
                mv $TMPFILE SHA1SUMS
				chmod 644 SHA1SUMS
                output "Info:   created SHA1SUMS in $DIR"
	        }
	    if test -n "$CREATE_META"; then
		test -e SHA1SUMS.meta || touch SHA1SUMS.meta
		sha1sum SHA1SUMS > $TMPFILE
		cmp -s $TMPFILE SHA1SUMS.meta || {
		    mv $TMPFILE SHA1SUMS.meta
			chmod 644 SHA1SUMS.meta
		    output "Info:   created SHA1SUMS.meta in $DIR"
		}
	    fi
            rm -f $TMPFILE
        fi
    done
done
