#! /bin/bash

#
# babarchive_prep_one
# Copyright (C) 2001-2016 by John Heidemann
#
# (was prep_cdrom)
#
#    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.
#
#    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.
#

usage () {
    cat 1>&2 <<END
usage: $0 [DIR...]

For each DIR, prepare it as the root of an archive tree.
Specifically:
	0. check for bogus files (.xvpics, *~)
	1. create a .shasum (or .sha1sum) file in each directory for validation
	2. create an index.html in the root
	3. create an ls-lR in the root
	4. create a .shasum.fsdb file in the root

options:
	-a ALG   specify algorithm (sha256 or sha1, defaults to sha256)
	-d debug
	-f force mode, fix problems and go on
        -j use parallel checksumming ($PARALLEL_SUMS by default)

This is a bash script to get bash handling of echo -e
END
    exit 1
}

warn () {
	echo "$@" 1>&2
}

die () {
	echo "$@" 1>&2
	exit 1
}

test "x$1" = 'x-?' && usage

SUBPROGRAM_DIR=$( cd -P `dirname $0`; pwd )

PARALLEL_SUMS=4
alg=auto
algprog=""
algargs=""
algprefix=auto
algsuffix=auto
debug=false
force=false
while getopts "a:dfj:" ch
do
        case $ch in
        ( a ) alg=$OPTARG;;
        ( d ) debug=true;;
        ( j ) parallel_sums=$OPTARG;;
        ( f ) force=true;;
        ( * ) usage;;
        esac
done
shift $(($OPTIND - 1))

case x$alg in
	xmd5)
		alg=md5; algprog=md5sum; algargs=""; algprefix=md5; algsuffix=jdb;;
	xsha1|x1)
		alg=1; algprog=sha1sum; algargs=""; algprefix=sha1; algsuffix=jdb;;
	xsha256|x256|xauto)
		alg=256; algprog=shasum; algargs="--binary -a $alg"; algprefix=sha; algsuffix=fsdb;;
	*)
		die "unknown algorithm $alg"
		;;
esac

tmpfile=/tmp/$$
trap 'rm -f $tmpfile; exit 1' 1 2 15
trap 'rm -f $tmpfile; exit 0' 0

doit () {
	DIR=$1

	#
	# 0a. check permissions
	#
	broken=false
	(
		cd $DIR || die "cannot cd $DIR"
		{
			find . -name .${algprefix}sum -print
			echo ls-lR
			echo index.html
		} | while read fn
		do
		        test -e "$fn" -a \! -w "$fn" && {
				warn "$0: cannot write to $fn"
				if $force
				then
					warn "    fixing"
					chmod u+w "$fn"
				else
					broken=true
				fi
			}
		done
		#
		find . -type d -print | while read d
		do
			test -w "$d" || {
				warn "$0: cannot write to $d"
				if $force
				then
					warn "    fixing"
					chmod u+w "$d"
				else
					broken=true
				fi
			}
		done
	)

	#
	# 0b. check for bogus files
	#
	(
		cd $DIR || die "cannot cd $DIR"
		find . \( -name \*~ -o -name rdista[0-9][0-9][0-9][0-9][0-9] \) -print |
		while read fn
		do
			warn "$0: bogus file $fn"
			if $force
			then
				rm "$fn"
			else
				broken=true
			fi
		done

		find . -name .xvpics -type d -print |
		    while read fn; do
			    warn "$0: bogus file $fn"
			    if $force; then
				    rm $fn/*
				    rmdir "$fn"
			    else
				    broken=true
			    fi
		    done
	)
	if $broken; then
		die "$0 aborted because of problems"
	fi

	test -f $DIR/.${algprefix}sum.${algsuffix} && rm -f $DIR/.${algprefix}sum.${algsuffix}
	#
	# 1. ${algprefix}sum
	#
	$SUBPROGRAM_DIR/babarchive_prep_directories -x -a $alg -j ${parellel_sums:-$PARALLEL_SUMS} $DIR ||
        die "$0 FAILED to babarchive_prep_directories $DIR" 


	#
	# 2. index.html
	#
	test -f $DIR/index.html ||
	(
		cd $DIR || die "cannot cd $DIR"
		cat >>index.html <<EOF
<html> <head>
<title>Babarchive Master Index</title>
</head><body>
<h1>Babarchive Master Index</h1>
<ul>
EOF
	#
        find . \( -name ALBUMS \) -print | sort | \
        while read pn
        do
                fpn="$pn"
                test -f "$pn/index.html" && fpn="$fpn/index.html"
                echo '<li> <a href="'"$fpn"'">'"$pn"'</a>' >>index.html
        done
	#
	find . -name index.html -print | sort |  \
	while read pn
	do
		fpn="$pn"
		echo '<li> <a href="'"$fpn"'">'"$pn"'</a>' >>index.html
	done
	#
	find . \( -name ORIG -o -name MASTER \) -print | sort  | \
	while read pn
	do
		fpn="$pn"
		echo '<li> <a href="'"$fpn"'">'"$pn"'</a>' >>index.html
	done
	cat >>index.html <<EOF
</ul>
</body></html>
EOF
	)
	#
	# 3. ls-lR
	#
	(
		cd $DIR || die "cannot cd $DIR"
		rm -f ls-lR 
		# do not include ls-lR in the file itself
		ls -lR |grep -v ^total >.ls-lR
		mv -f .ls-lR ls-lR
	)

	#
	# fix-up root ${alg}sum
	#
	$SUBPROGRAM_DIR/babarchive_prep_directories -a $alg -j ${parellel_sums:-$PARALLEL_SUMS} -n $DIR

	#
	# do master ${alg}sum.fsdb file
	#
	$SUBPROGRAM_DIR/babarchive_prep_topshasum -a $alg -s $DIR
}

for arg in $*; do
    ( 
        cd -P $arg || die "$0: Can't cd $arg"
        doit $PWD
    )
done

exit 0
