#!/bin/sh

# create_redundancy_iso.sh
# 2011-06-17
# by Gernot WALZL

# This script creates an iso image from the contents of a given directory.
# The contents will be replicated as often as it is possible to
# still fit on a single disc.
# This is done in hope that the contents live forever.
#
# Note: The Joliet filenames are specified in Unicode.
# Each path component can be up to 64 Unicode characters long.


TMP=${TMP:-/tmp/redundancy}
SIZE_TARGET=${SIZE_TARGET:-700}

DIR="$1"
if [ ! -d "$DIR" ]; then
  echo "Error: a directory is required as first parameter"
  exit 1
fi


SIZE_DIR=$(du -ms "$DIR" | awk '{print $1}')
NUM_COPIES=$(($SIZE_TARGET/$SIZE_DIR))

rm -rf "$TMP"
mkdir -p "$TMP"

cp "$0" "$TMP"
cat > "$TMP/README.txt" <<EOF
Every folder in the root directory of this disc
contains exactly the same contents.
May the contents live forever.
EOF

for I in $(seq -w $NUM_COPIES); do
  cp -r "$DIR" "$TMP/$I"
done

VOLID=$(basename "$DIR")
mkisofs -iso-level 2 -J -r -V "$VOLID" -o "${VOLID}.iso" "$TMP"
rm -rf "$TMP"