#!/bin/sh

# Make a test filesystem.
#
# Copyright (C) 2021, 2022, 2024 Paul Boddie <paul@boddie.org.uk>
#
# 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

PROGNAME=$(basename "$0")

# Handle program options.

if [ "$1" = '-q' ] ; then
    QUIET=$1
    shift 1
else
    QUIET=
fi

PKGDIR=$(realpath "$1")
E2ACCESS_DIR=$(realpath "$2")
TARGET=$(realpath "$3")

shift 3

if [ ! -e "$PKGDIR" ] || [ ! -e "$E2ACCESS_DIR" ] || [ ! "$TARGET" ] ; then
    cat 1>&2 <<EOF
Usage: $PROGNAME [ -q ] <package directory> <e2access directory> <target> [ <program> ... ]

Package directory: $PKGDIR
e2access directory: $E2ACCESS_DIR
Target filesystem: $TARGET
Programs: $*
EOF
    exit 1
fi

# Initialise the program details and environment.

E2ACCESS="$E2ACCESS_DIR"/e2access
export LD_LIBRARY_PATH="$E2ACCESS_DIR"

OPTIONS='-u 1000 -g 1000'

# Test parameters.
# See: tests/dstest_file_readdir.cfg

MANY_FILES_COUNT=400

# Define a convenience function for e2access invocation.

e2access()
{
    "$E2ACCESS" $OPTIONS "$TARGET" $*
}

# Make a filesystem for the example.

mkdir tmp_e2test
cd tmp_e2test

# Set a sensible umask.

umask 0022

mkdir -p home/paulb/public
echo "Public!" > home/paulb/public/message

# Set a restrictive umask.

umask 0077

echo "Not public!" > home/paulb/public/message2

mkdir -p home/paulb/private
echo "Private!" > home/paulb/private/message

# Set a permissive umask.

umask 0000

mkdir -p home/paulb/shared
echo "Editable!" > home/paulb/shared/message

# Restore the umask for the remaining files.

umask 0022

mkdir -p home/paulb/many
cd home/paulb/many

# Populate the directory with plenty of files.

for N in `seq 1 $MANY_FILES_COUNT`; do
    echo "Contents of #$N." > "file-$N".txt
done

cd ..

# Put file in the created home directory.

cp "$PKGDIR/../docs/LICENCE.txt" .

# Return to the top level.

cd ../..

# Create a top-level bin directory.

mkdir bin

# Put some programs in the bin directory.

for PROGRAM in $* ; do
    cp $(realpath "$PROGRAM") bin
done

# Create a filesystem image.

EXTRA=2000
SIZE=$(du -s -k . | cut -f 1)
TOTAL=$(($SIZE + $EXTRA))

dd if=/dev/zero of="$TARGET" bs=1024 count=$TOTAL

if ! $(/sbin/mkfs.ext2 -q "$TARGET") ; then
    exit 1
fi

# Add the directories and files to the image.

for DIR in bin home/paulb home/paulb/private home/paulb/public home/paulb/shared home/paulb/many ; do
    e2access mkdir "$DIR"
    e2access copy-in $(find "$DIR" -maxdepth 1 -type f | sort) "$DIR"
done

# Leave the root of the filesystem.

cd ..

if [ ! "$QUIET" ] ; then
    e2access ls ''
    e2access ls 'bin'
    e2access ls 'home'
    e2access ls 'home/paulb'
    e2access ls 'home/paulb/many'
    e2access ls 'home/paulb/private'
    e2access ls 'home/paulb/public'
    e2access ls 'home/paulb/shared'
fi

rm -r tmp_e2test

# vim: tabstop=4 expandtab shiftwidth=4
