#!/bin/sh -e
#
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
# 2013 Steven Armstrong (steven-cdist armstrong.cc)
# 2014 Daniel Heule (hda at sfs.biz)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# 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"
state_should=$(cat "$__object/parameter/state")
type=$(cat "$__object/explorer/type")
stat_file="$__object/explorer/stat"

# variable to keep track if we have to set directory attributes
set_attributes=

mkdiropt=""
[ -f "$__object/parameter/parents" ] && mkdiropt="-p"

recursive=""
if [ -f "$__object/parameter/recursive" ]; then
   recursive="-R"
   # need to allways set attributes when recursive is given
   # as we don't want to check all subfolders/files
   set_attributes=1
fi

get_current_value() {
   if [ -s "$stat_file" ]; then
      _name="$1"
      _value="$2"
      case "$_value" in
         [0-9]*)
            _index=2
         ;;
         *)
            _index=3
         ;;
      esac
      awk '/'"$_name"':/ { print $'$_index' }' "$stat_file"
      unset _name _value _index
   fi
}

set_group() {
    echo "chgrp $recursive '$1' '$destination'"
    echo "chgrp $recursive '$1'" >> "$__messages_out"
}

set_owner() {
    echo "chown $recursive '$1' '$destination'"
    echo "chown $recursive '$1'" >> "$__messages_out"
}

set_mode() {
    echo "chmod $recursive '$1' '$destination'"
    echo "chmod $recursive '$1'" >> "$__messages_out"
}

case "$state_should" in
   present|exists)
      if [ "$type" != "directory" ]; then
         set_attributes=1
         if [ "$type" != "none" ]; then
            # our destination is not a directory, remove whatever is there
            # and then create our directory and set all attributes
            echo "rm -f '$destination'"
            echo "remove non directory" >> "$__messages_out"
         fi
         echo "mkdir $mkdiropt '$destination'"
         echo "create" >> "$__messages_out"
      elif [ "$state_should" = 'exists' ]; then
         # The type is directory and --state exists. We are done and do not
         # check or set the attributes.
         exit 0
      fi

      # Note: Mode - needs to happen last as a chown/chgrp can alter mode by
      # clearing S_ISUID and S_ISGID bits (see chown(2))
      for attribute in group owner mode; do
         if [ -f "$__object/parameter/$attribute" ]; then
            value_should="$(cat "$__object/parameter/$attribute")"
            value_is="$(get_current_value "$attribute" "$value_should")"

            # format mode in four digits => same as stat returns
            if [ "$attribute" = mode ]; then
                # Convert to four-digit octal number (printf interprets
                # strings with leading 0s as octal!)
                value_should=$(printf '%04o' "0${value_should}")
            fi

            if [ "$set_attributes" = 1 ] || [ "$value_should" != "$value_is" ]; then
               "set_$attribute" "$value_should"
            fi
         fi
      done
   ;;
   pre-exists)
      case $type in
         directory)
            # all good
            exit 0
         ;;
         none)
            printf 'Directory "%s" does not exist\n' "$destination" >&2
            exit 1
         ;;
         file|symlink)
            printf 'File "%s" exists and is a %s, but should be a directory\n' "$destination" "$type" >&2
            exit 1
         ;;
         *)
            printf 'File or directory "%s" is in an unknown state\n' "$destination" >&2
            exit 1
         ;;
      esac
   ;;
   absent)
        if [ "$type" = "directory" ]; then
            echo "rm -rf '$destination'"
            echo remove >> "$__messages_out"
        fi
   ;;
   *)
      echo "Unknown state: $state_should" >&2
      exit 1
   ;;
esac
