#!/bin/sh
# Miscellaneous tests for "ln".

# Copyright (C) 1998, 1999, 2000, 2004, 2006 Free Software Foundation, Inc.

# 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.

if test "$VERBOSE" = yes; then
  set -x
  ln --version
fi

pwd=`pwd`
tmp=t2-ln.$$
trap 'status=$?; cd "$pwd" && rm -rf $tmp && exit $status' 0
trap '(exit $?); exit' 1 2 13 15

framework_failure=0
mkdir $tmp || framework_failure=1
cd $tmp || framework_failure=1

t=tln-symlink
d=tln-subdir
ld=tln-symlink-to-subdir
f=tln-file
fail=0

# Create a simple symlink with both source and destination files
# in current directory.
touch $f || framework_failure=1
rm -f $t || framework_failure=1
ln -s $f $t || fail=1
test -f $t || fail=1
rm $t $f

# Create a symlink with source file and explicit destination directory/file.
touch $f || framework_failure=1
rm -rf $d || framework_failure=1
mkdir $d || framework_failure=1
ln -s ../$f $d/$t || fail=1
test -f $d/$t || fail=1
rm -rf $d $f

# Create a symlink with source file and destination directory.
touch $f || framework_failure=1
rm -rf $d || framework_failure=1
mkdir $d || framework_failure=1
ln -s ../$f $d || fail=1
test -f $d/$f || fail=1
rm -rf $d $f

# See whether a trailing slash is followed too far.
touch $f || framework_failure=1
rm -rf $d || framework_failure=1
mkdir $d $d/$f || framework_failure=1
ln $f $d/ 2> /dev/null && fail=1
ln -s $f $d/ 2> /dev/null && fail=1
rm -rf $d $f

# Make sure we get a failure with existing dest without -f option
touch $t || framework_failure=1
# FIXME: don't ignore the error message but rather test
# it to make sure it's the right one.
ln -s $t $t 2> /dev/null && fail=1
rm $t

# Make sure -sf fails when src and dest are the same
touch $t || framework_failure=1
ln -sf $t $t 2> /dev/null && fail=1
rm $t

# Create a symlink with source file and no explicit directory
rm -rf $d || framework_failure=1
mkdir $d || framework_failure=1
touch $d/$f || framework_failure=1
ln -s $d/$f || fail=1
test -f $f || fail=1
rm -rf $d $f

# Create a symlink with source file and destination symlink-to-directory.
rm -rf $d $f $ld || framework_failure=1
touch $f || framework_failure=1
mkdir $d || framework_failure=1
ln -s $d $ld
ln -s ../$f $ld || fail=1
test -f $d/$f || fail=1
rm -rf $d $f $ld

# Create a symlink with source file and destination symlink-to-directory.
# BUT use the new --no-dereference option.
rm -rf $d $f $ld || framework_failure=1
touch $f || framework_failure=1
mkdir $d || framework_failure=1
ln -s $d $ld
af=`pwd`/$f
ln --no-dereference -fs "$af" $ld || fail=1
test -f $ld || fail=1
rm -rf $d $f $ld

# Try to create a symlink with backup where the destination file exists
# and the backup file name is a hard link to the destination file.
touch a b || framework_failure=1
ln b b~ || framework_failure=1
ln -f --b=simple a b || fail=1

# ===================================================
# determine if link(2) follows symlinks on this system
touch a || framework_failure=1
ln -s a symlink || framework_failure=1
ln symlink hard-to-sym > /dev/null 2>&1 || framework_failure=1
ls=`ls -lG hard-to-sym`x
case "$ls" in
  *'hard-to-symx') link_follows_symlink=yes ;;
  *'hard-to-sym -> ax') link_follows_symlink=no ;;
  *) framework_failure=1 ;;
esac

if test $link_follows_symlink = no; then
  # Create a hard link to a dangling symlink.
  # This is not portable.  At least sunos4.1.4 and OpenBSD 2.3 fail this test.
  # They get this:
  # ln: cannot create hard link `hard-to-dangle' to `no-such-dir': \
  #   No such file or directory
  #
  ln -s /no-such-dir || fail=1
  ln no-such-dir hard-to-dangle > /dev/null 2>&1 || fail=1
fi
rm -rf a symlink hard-to-sym hard-to-dangle
# ===================================================

# Make sure ln can make simple backups.
# This was fixed in 4.0.34.  Broken in 4.0r.
for cmd in ln cp mv ginstall; do
  rm -rf a x a.orig
  touch a x || framework_failure=1
  $cmd --backup=simple --suffix=.orig x a || fail=1
  test -f a.orig || fail=1
done

# ===================================================
# With coreutils-5.2.1, this would mistakenly access argv[1][-1].
# I'm including it here, in case some day programs like valgrind detect that.
# Purify probably would have done so.
ln foo '' 2> /dev/null

# ===================================================

if test $framework_failure = 1; then
  echo 'failure in testing framework' 1>&2
  exit 1
fi

exit $fail
