#!/bin/sh
# ls
# Wrapper to handle long-iso time format requested by makepkg.

LS=/bin/ls

if ${LS} --version 2>/dev/null | grep -q GNU || \
   ! echo "$*" | grep -q -- "--time-style=long-iso"
then
   exec ${LS} "$@"
else

   # Parse cmdline
   while [ -n "${1}" ]; do
      case ${1} in
      --time-style=long-iso)
         ;;

      -*)
         SWITCHES="${SWITCHES} ${1}"
         ;;

      *)
         FILENAMES="${FILENAMES} ${1}"
         ;;
       esac
   shift 1
   done

   # Print stuff on screen.
   for FILE in ${FILENAMES} ; do
      FRONTLOAD="$(stat -c '%A %h %U %G %s ' "${FILE}" )"
      TIMESTAMP="$(stat -c '%z ' "${FILE}" | cut -f 1 -d .)"
      BACKLOAD="$(stat -c '%n' "${FILE}" )"
      if [ -L "${FILE}" ]; then
         BACKLOAD="${BACKLOAD} -> $(readlink "${FILE}")"
      fi
      echo "${FRONTLOAD} ${TIMESTAMP} ${BACKLOAD}"
   done
fi
