#!/bin/bash

echo "Running on $(grep ^PRETTY_NAME /etc/os-release|cut -d'"' -f2)"
echo "TEST 1 - trailing colon syntax ignored"

rm -f ownergroup_test

echo touch ownergroup_test
touch ownergroup_test

echo ls -l ownergroup_test
ls -l ownergroup_test

echo chown perforce: ownergroup_test
chown perforce: ownergroup_test

echo ls -l ownergroup_test
ls -l ownergroup_test

Owner=$(stat -c %U ownergroup_test 2>/dev/null)
Group=$(stat -c %G ownergroup_test 2>/dev/null)

echo "The owner/group after 'chown perforce: ownergroup_test' should be perforce:perforce."
if [[ "$Owner":"$Group" == "perforce:perforce" ]]; then
   echo "PASS TEST 1: Ownership is correct."
else
   echo "FAIL TEST 1: Ownership is incorrect; it is $Owner:$Group."
fi

echo "TEST 2 - chown -h fails for symlink to directory"

rm -rf dir1
rm -f symlink_to_dir1
echo mkdir dir1
mkdir dir1

echo ln -s dir1 symlink_to_dir1
ln -s dir1 symlink_to_dir1

echo ls -ld dir1 symlink_to_dir1
ls -ld dir1 symlink_to_dir1

echo chown -h perforce symlink_to_dir1
chown -h perforce symlink_to_dir1

echo ls -ld dir1 symlink_to_dir1
ls -ld dir1 symlink_to_dir1

DirOwner=$(stat -c %U dir1 2>/dev/null)
LinkOwner=$(stat -c %U symlink_to_dir1 2>/dev/null)

echo "The owner of the symlink and dir after 'chown -h symlink_to_dir1' should be perforce (symlink) and root (dir), respectively."
if [[ "$LinkOwner":"$DirOwner" == "perforce:root" ]]; then
   echo "PASS TEST 2: Link and Dir Ownership is correct."
else
	echo "FAIL TEST 2: Link and Dir Ownership is incorrect; it is '$LinkOwner' (symlink) and '$DirOwner' (dir)."
fi
