#!/bin/bash

usage="\
Usage:
  $(basename $0) [commands-path] [test-volume]

Arguments:
  commands-path (default to ../scripts)
  test-voluem   (default to test-volume)

Run the test suite on trash-cli commands founds in <commands-path> using the 
specified <test-volume>. Use --help for more informations.

TEST VOLUME
-----------
The <test-volume> is supplied by the user and it is used by the tests to verify
the supports of the \$topdir trash directories. 
The user supplied test-volume must satisfy these requirements:
 * the test-volume shall be a mount point 
 * the test-volume shall be different from the the volume of the current dir
 * the test-volume couldn't contain any .Trash or Trash-\$uid entry in its 
   \$topdir

The latest is for security because the tests tends to destroy the 
trash directories found in the test-volume.

If you are a sudoers, you can use the create-test-volume script to create a 
test-volume. If you aren't you should create a test volume by yourself or 
use an existing volume. 

WARNING: Using an existing volume as test volume is BAD idea because it may 
cause a data loss.

COMMANDS PATH
-------------
The commands-path arguments specify where are the trash-cli commands that should
be tested.

USAGE EXAMPLE
-------------

 $ ./prepare-test-volume-linux
 [sudo] password for andrea: **********
 ...
 test-volume mounted as './test-volume'

 $ ./run-tests ../scripts test-volume
"

# Run all tests in each *test.bash files 

setup-commands-wrappers() {
    base="${1:?"Base not specified. Usage: setup-commands-wrappers <base>"}"
    echo "Executable location: $base"
    
    for command in trash-put trash-empty trash-list \
                   trash-restore restore-trash; do
        echo "Creating wrapper for $command ..."

        if [ ! -x "$base/$command" ]; then
	    echo "Not executable: $base/$command" 1>&2;
        fi
        
        wrapper_def="
	function _$command () {
		echo \"Invoking $base/$command $@\" 1>&2
		\"$base/$command\" \"\$@\"
	}"
        echo "$wrapper_def"
	eval "$wrapper_def"
    done
}


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

# Creates the $topdir variable so test script can refert to it.
# The $topdir variabile should point to an alternative volume. 
setup-test-volume() {
    topdir="${1:?"Missing arguments. Usage $0 <volume-path>"}"
    if [ ! -e "$topdir" ]; then
        error "test-volume does not exist: $topdir" 1>&2
        exit 1
    fi

    if [ -e "$topdir/.Trash" ]; then
        error "test-volume should not contains the .Trash dir: $topdir/.Trash"
        exit 1
    fi
    
    for i in $topdir/.Trash-*; do
        if [ -e "$i" ]; then
            error "test-volume should not contains any /.Trash-\$uid dir: $i"
            exit 1
        fi
    done
}


commands_path="${1:-"../scripts"}"; shift
test_volume="${1:-"test-volume"}"; shift

# setup the enviroment
setup-commands-wrappers "$commands_path"
setup-test-volume "$test_volume"
export XDG_DATA_HOME="./sandbox/XDG_DATA_HOME"

# load all tests definitions
for i in "$(dirname "$0")/"*test.bash; do 
    echo "Loading $i ..."
    . "$i"
done

# run all tests
. "$(dirname "$0")/bashunit.bash"
