#!/bin/sh

set -e

cd ${AUTOPKGTEST_TMP}

# Testing run-parts
run-parts --version
run-parts --help

mkdir Run-parts && cd Run-parts
touch file executable1
echo "#! /bin/sh" >> executable1
echo "ls -la file" >> executable1
cp executable1 executable2
chmod 755 executable1 executable2

list=$(run-parts --list .)
echo "$list"
test=$(run-parts --test .)
echo "$test"
if [ "$list" != "$test" ]; then
	echo "Success! 'run-parts --test' only runs on executables"
fi

run-parts -v . 2>&1
run-parts --report --reverse .

# Testing which
mkdir ../Which && cd ../Which
which sh

PATH="$PATH:/tmp"
cp /usr/bin/which /tmp
with_a_arg=$(which -a which)
echo "$with_a_arg"
without_a_arg=$(which which)
echo "$without_a_arg"
if [ "$with_a_arg" != "$without_a_arg" ]; then
	echo "Success! 'which -a' works as expected"
fi

which fakeCommand 2>&1 || echo "Success! 'which fakeCommand' does not exist"

which -f sh 2>&1 || echo "Success! '-f' is an invalid option"

# Testing ischroot
ischroot --version
ischroot --help

# Testing tempfile
mkdir ../Tempfile && cd ../Tempfile
tempfile --version 2>&1
tempfile --help 2>&1

tempFile=$(tempfile) 2>&1
ls -la "$tempFile" && \
	echo "Success! Temporary file created"

tempFile=$(tempfile -n temporary) 2>&1
ls -la "$tempFile" | grep -- "temporary" && \
	echo "Success! Name was set properly"

tempFile=$(tempfile -p my_ -s _file) 2>&1
ls -la "$tempFile" | grep -- "my_" && \
	echo "Success! Prefix was set properly"
ls -la "$tempFile" | grep -- "_file" && \
	echo "Success! Suffix was set properly"

mkdir tmp
tempFile=$(tempfile -d tmp) 2>&1
ls -la "$tempFile" | grep -- "tmp/" && \
	echo "Success! tempfile created in 'tmp/' directory"

tempFile=$(tempfile -m 644) 2>&1
ls -la "$tempFile" | grep -- "-rw-r--r--" && \
	echo "Success! File permissions set as 644, not 600"

# Testing savelog
mkdir ../Savelog && cd ../Savelog

savelog -h

savelog -t temp
ls -la temp
ls -la temp.0

ls -la temp | grep -- "-rw-r--r--" && \
	echo "Log file permissions set to 644"
savelog -m 755 temp
ls -la temp.1.gz
ls -la temp | grep -- "-rwxr-xr-x" && \
	echo "Success! Log file permissions changed to 755"

savelog temp
ls -la temp.1.gz && ls -la temp.2.gz && \
	echo "Success! Log file was rotated and compressed"

savelog -l temp
ls -la temp.1.gz 2>&1 || ls -la temp.1 && \
	ls -la temp.2.gz && ls -la temp.3.gz && \
	echo "Success! Log file was rotated and not compressed"
