#!/bin/sh
#
# 2012-2014 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist 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 3 of the License, or
# (at your option) any later version.
#
# cdist 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 cdist. If not, see <http://www.gnu.org/licenses/>.
#

destination="/$__object_id"
type="$(cat "$__object/parameter/type")"
source="$(cat "$__object/parameter/source")"

# no destination? -> state is absent
if [ ! -e "$destination" ]; then
   echo absent
   exit 0
fi

destination_dir="${destination%/*}"

case "$type" in
   symbolic)
      cd "$destination_dir" || exit 1
      if [ -h "$destination" ]; then
         source_is=$(readlink "$destination")
         # ignore trailing slashes for comparison
         if [ "${source_is%/}" = "${source%/}" ]; then
            echo present
         else
            echo wrongsource
         fi
      else
         echo absent
      fi
   ;;
   hard)
      cd "$destination_dir" || exit 1
      # check source relative to destination_dir
      if [ ! -e "$source" ]; then
         echo sourcemissing
         exit 0
      fi
      # Currently not worth the effor to change it, stat is not defined by POSIX
      # and different OSes has different implementations for it.
      # shellcheck disable=SC2012
      destination_inode=$(ls -i "$destination" | awk '{print $1}')
      # Currently not worth the effor to change it, stat is not defined by POSIX
      # and different OSes has different implementations for it.
      # shellcheck disable=SC2012
      source_inode=$(ls -i "$source" | awk '{print $1}')
      if [ "$destination_inode" -eq "$source_inode" ]; then
         echo present
      else
         echo absent
      fi
   ;;
   *)
      echo "Unknown type: $type" >&2
      exit 1
   ;;
esac
