#!/bin/sh
# $Id: do-unknown,v 1.2 2001/07/10 18:56:57 rbasch Exp $

. /mit/mimeutils/share/scripts/utils.sh

name=

usage() {
  echo "Usage: do-unknown [-n name] <file>" 1>&2
  exit 1
}

while getopts n: opt; do
  case "$opt" in
    n)
      name="$OPTARG"
      ;;
    \?)
      usage
      ;;
  esac
done
shift `expr "$OPTIND" - 1`

# Check for a .xls file and hope it's always Excel, since we might lose later.
case "$name" in
  *.xls)
    exec do-excel "$1"
    ;;
esac

# First look for magic numbers
type=`file -m /mit/mimeutils/share/magic.mime "$1" | sed -e 's/.*: //'`
case "$type" in
  ""|data)
    ;;

  empty)
    becho Empty file
    exit
    ;;

  *\ text)
    ttyprompt display text
    do-text -s plain "$1" | "$PAGER"
    ttyprompt continue
    exit
    ;;

  *)
    basetype="`echo $type | awk -F/ '{print $1;}'`"
    subtype=`echo $type | awk -F/ '{print $2;}'`
    exec do-$basetype -s "$subtype" "$1"
    ;;

esac

# If that fails, see if we have filename information for things
# that we can't identify otherwise.
case "$name" in
  *.tga)
    exec do-image -s targa "$1"
    ;;

  *.pcx)
    exec do-image -s x-pcx "$1"
    ;;

esac

# We can't recognize Word 6 and 7 documents reliably
format=`elser -i "$1" 2>&1 | sed -n -e 's/.*: \(word[678]\),.*/\1/p'`
case "$format" in
  word*)
    exec do-msword "$1"
    ;;
esac

# We lose. Try the full magic now to present to the user.
type=`file -m /mit/mimeutils/share/magic "$1" | awk -F" " '{print $2;}'`

case "$name" in
  "")
    forfile=""
    ;;
  *)
    forfile=" for file \"$name\""
    ;;
esac

case "$type" in
  data|"")
    becho Could not identify data$forfile.
    ;;

  *)
    becho Could not display data$forfile.
    becho Data appears to be of type \"$type\".
    ;;
esac

exit 1
