#!/bin/bash

# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"
#set -x

echo file1 > file
testing "ln create_hardlink" "ln file hlink && [ file -ef hlink ] &&
          echo 'yes'" "yes\n" "" ""
testing "ln create_softlink" "ln -s file slink && [ -L slink ] &&
          readlink slink" "file\n" "" ""
rm slink hlink

echo hlink1 > hlink
testing "ln force_create_hardlink" "ln -f file hlink &&
          [ file -ef hlink ] && cat hlink 2>/dev/null" "file1\n" "" ""

echo slink1 > slink
testing "ln force_create_softlink" "ln -f -s file slink &&
          [ -L slink ] && readlink slink" "file\n" "" ""
rm slink hlink

echo hlink1 > hlink
set +e
testing "ln preserves_hardlinks" "ln file hlink 2>/dev/null || echo 'yes'" \
          "yes\n" "" ""

echo slink1 > slink
set +e
testing "ln preserves_softlinks" "ln -s file slink 2>/dev/null || echo 'yes'" \
          "yes\n" "" ""
rm slink hlink

mkdir dir
testing "ln multilevel_symbolic_links" "ln -s dir slink &&
          ln -s file slink && [ -L slink -a -L slink/file ] &&
          readlink slink && readlink slink/file" "dir\nfile\n" "" ""
rm slink

testing "ln no_dereference" "ln -s dir slink &&
          ln -n -s file slink 2>/dev/null || [ -L slink ] && readlink slink" \
          "dir\n" "" ""
rm -rf file dir slink

touch file1 file2 && mkdir dir
testing "ln create_multiple_hardlinks" "ln file* dir/ &&
   [ file1 -ef dir/file1 -a file2 -ef dir/file2 ] && echo 'yes'" "yes\n" "" ""
rm -rf file* dir

touch file1 file2 && mkdir dir
testing "ln create_multiple_softlinks" "ln -s file* dir/ &&
          [ -L dir/file1 -a -L dir/file2 ] && readlink dir/file1 &&
          readlink dir/file2" "file1\nfile2\n" "" ""
rm -rf file* dir

echo file1 > file
testing "ln create_softlink_and_remove_sourcefile" "ln -s file slink &&
          [ -L slink ] && rm file && cat slink 2>/dev/null || echo 'yes' " \
          "yes\n" "" ""
rm -f file slink

echo file1 > file
testing "ln create_hardlink_and_remove_sourcefile" "ln file hlink &&
          [ file -ef hlink ] && rm file && [ -f hlink ] && echo 'yes'" \
          "yes\n" "" ""
rm -f file hlink