#!/bin/ash
#200801 needed by nnn, shfm, console file managers.

NAMEonly="$(basename "${@}")"

#code below is /sbin/xdg-open in the initrd. in main f.s. we have file...
which file >/dev/null
if [ $? -eq 0 ];then
 MIMETYPE="$(file -bi "$@")"
 case "$MIMETYPE" in
  text*)
   mp "$@"
   exit 0
  ;;
 esac
 exit 1
fi

case "$NAMEonly" in
 *.txt|*.doc|*.h|*.log|*.bac|*.c|*.cpp|*.sh|*.conf|*.cfg|*.xml)
  mp "$@"
 ;;
 *gz|*xz|*bz2|*.zip|*.sfs)
  exit 1
 ;;
 *.png|*.gif|*.jpg)
  exit 1
 ;;
 *)
  SHEBANG="$(dd if="$@" status=none bs=1 count=3)"
  if [ "$SHEBANG" == "#!/" ];then
   mp "$@"
  else
   #printable ascii \040-\177 (32-127 decimal), \012=LF, \015=CR (10, 13 dec.)
   NONTXT="$(cat -n 1 "${@}" | tr -d '[\040-\177\012\015]' | wc -c)"
   if [ "$NONTXT" == "0" ];then
    mp "$@"
   else
    exit 1
   fi
  fi
esac

exit 0
###end###
