#!/usr/bin/bash

# File:  test-whether-unison-preserves-execute-permissions.bash

# Test whether unison (and the filesystem housing /tmp)
# synchronizes execute-bits on new, local files.

# Comment out to turn on script debugging:
#set -x

# We need to clean up after the test is done, so we use this flag to 
# determine the exit status later.
#
success="false"

umask 007

# Try to create a temporary directory to store all test files.
#
tmpDir="/tmp/unison.$$"
if ! mkdir "${tmpDir}"; then
  echo >&2 "${0}": cannot make temporary directory.
  exit 1
fi

export UNISON="${tmpDir}/.unison"  # Keep ~/.unison clean.
mkdir "${UNISON}"

mkdir "${tmpDir}/a" "${tmpDir}/b"
touch "${tmpDir}/a/foo"
chmod u+rwx,go= "${tmpDir}/a/foo"

echo "----- Before synchronization:"
ls -lRa "${tmpDir}"
echo

echo "----- Running unison."
unison -batch -debug all -log=false -perms 0o1777 "${tmpDir}/a" "${tmpDir}/b" 
echo

echo "----- After synchronization:"
ls -lRa "${tmpDir}"
echo

if [ -x "${tmpDir}/b/foo" ]; then
  success="true"
else
  echo >&2 "${0}: test failed (for filesystem housing /tmp): execute bit not preserved"
  success="false"
fi

# Clean up the temporary directory we made.
rm -r "${tmpDir}"

if [ "${success}" = "false" ]; then
  exit 1
fi

