# This file is sourced by init.sh, *before* its initialization.

# This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
# TESTS_ENVIRONMENT definition.
stderr_fileno_=9

# For repeatability, reset the environment to known value.
LANG=C
LC_ALL=C
TZ=UTC
export LANG LC_ALL TZ
export PARTED_SUPPRESS_FILE_SYSTEM_MANIPULATION_WARNING=1

# Protect ourselves from common misconfiguration to export
# CDPATH into the environment
unset CDPATH

# Suppress readline initialization garbage.
unset TERM

sector_size_=${PARTED_SECTOR_SIZE:-512}

require_512_byte_sector_size_()
{
  test $sector_size_ = 512 || skip_ 'FS test with sector size != 512'
}

# Having an unsearchable directory in PATH causes execve to fail with EACCES
# when applied to an unresolvable program name, contrary to the desired ENOENT.
# Avoid the problem by rewriting PATH to exclude unsearchable directories.
# Also, if PATH lacks /sbin and/or /usr/sbin, append it/them.
sanitize_path_()
{
  # FIXME: remove double quotes around $IFS when all tests use init.sh.
  # They constitute a work-around for a bug in FreeBSD 8.1's /bin/sh.
  local saved_IFS="$IFS"
    IFS=:
    set -- $PATH
  IFS=$saved_IFS

  local d d1
  local colon=
  local new_path=
  for d in "$@"; do
    test -z "$d" && d1=. || d1=$d
    if ls -d "$d1/." > /dev/null 2>&1; then
      new_path="$new_path$colon$d"
      colon=':'
    fi
  done

  for d in /sbin /usr/sbin ; do
    case ":$new_path:" in
      *:$d:*) ;;
      *) new_path="$new_path:$d" ;;
    esac
  done

  PATH=$new_path
  export PATH
}

require_udevadm_settle_()
{
  udevadm --help > /dev/null \
    || skip_ 'udevadm command is required for this test'
}

require_perl_digest_crc_()
{
  local ok=0
  local t
  t=$(perl -le 'use Digest::CRC qw(crc32); print crc32("123")') \
    && test $t = 2286445522 && ok=1
  test $ok = 1 \
    || skip_ "this test requires Perl's Digest::CRC module"
}

# We need two cleanup functions.  One, cleanup_final_, is sometimes
# used (transparently) via t-local.sh's modprobe/rmmod code.
# The other is used e.g., to unmount.
cleanup_final_() { :; }
cleanup_fn_() { :; }
cleanup_() { cleanup_fn_; cleanup_final_; }

# Use this for a test that requires an actual hardware device, e.g., a real
# disk, a USB key, or a CD-RW.  The envvars $DEVICE_TO_ERASE and
# $DEVICE_TO_ERASE_SIZE must be set properly.  Otherwise, skip the test.
require_erasable_()
{
  # Skip quietly if both envvars are not specified.
  test -n "$DEVICE_TO_ERASE" && test -n "$DEVICE_TO_ERASE_SIZE" \
    || skip_ 'This test requires an erasable device and you have not properly' \
        'set the $DEVICE_TO_ERASE and $DEVICE_TO_ERASE_SIZE envvars.'

  # Since testing a drive with parted destroys all data on that drive,
  # we have rather draconian safety requirements that should help avoid
  # accidents.  If $dev_ is the name of the device,
  # - running "parted -s $dev_ print" must succeed, and
  # - its output must include a line matching /^Disk $dev_: $DEV_SIZE$/
  # - Neither $dev_ nor any $dev_[0-9]* may be mounted.
  dev_=$DEVICE_TO_ERASE
  sz=$DEVICE_TO_ERASE_SIZE
  parted_output=$(parted -s $dev_ print) || fail_ "no such device: $dev_"
  parted -s $dev_ print|grep "^Disk $dev_: $sz$" \
    > /dev/null || fail_ "actual device size is not $sz"
  # Try to see if $dev_ or any of its partitions is mounted.
  # This is not reliable.  FIXME: find a better way.
  # Maybe expose parted's own test for whether a disk is in use.
  # The following assume that $dev_ is canonicalized, e.g., that $dev_
  # contains no "//" or "/./" components.

  # Prefer df --local, if it works, so we don't waste time
  # enumerating lots of automounted file systems.
  ( df --local / > /dev/null 2>&1 ) && df='df --local' || df=df
  $df | grep "^$dev_" && fail_ "$dev_ is already mounted"
  $df | grep "^$dev_[0-9]" && fail_ "a partition of $dev_ is already mounted"
}

# At least Fedora 16 (kernel 3.1.6-1.fc16.x86_64) fails this test.
require_partitionable_loop_device_()
{
  local f=/sys/devices/virtual/block/$(basename $1)/ext_range
  case $(cat "$f") in
    ''|0|1) skip_ your system is not configured to partition loop devices;;
  esac
}

sanitize_path_

. "$abs_top_srcdir/tests/t-lib-helpers.sh"
. "$abs_top_srcdir/tests/t-local.sh"
