#!/bin/sh # # Infer MIME type from file(1) output. # - Cameron Simpson 14nov2002 # : ${OS:=`uname -s|tr '[A-Z]' '[a-z]'`} cmd=$0 usage="Usage: $cmd filename Exit status 0 => Output is MIME type. Exit status 1 => Output is file(1) output." [ $# = 1 ] || { echo "$usage" >&2; exit 2; } file=$1; shift case "$OS" in sunos|solaris) lopt= ;; # standard file(1) *) lopt=-L ;; # hope for GNUish file(1) esac type=`file $lopt "$file" | tr ' ' ' '` || exit 1 mtype= case "$type" in *': '*'Bourne shell script'*) mtype=application/x-sh ;; *': '*'JPEG image data'*) mtype=image/jpeg ;; *': '*'GIF image data'*) mtype=image/gif ;; *': '*'PNG image data'*) mtype=image/png ;; *': '*'PDF document'*) mtype=application/pdf ;; *': '*'Adobe Portable Document Format'*) mtype=application/pdf ;; *': '*'RPM v'*) mtype=application/x-redhat-package-manager ;; *': '*'HTML document text'*) mtype=text/html ;; *': '*'PostScript document'*) mtype=application/postscript ;; *': '*'PDF document'*) mtype=application/pdf ;; *': '*'gzip compressed data'*) mtype=application/x-gzip ;; *': '*'bzip2 compressed data'*) mtype=application/x-bzip ;; *': '*'compressed data block compressed') mtype=application/x-compress ;; *': '*"compress'd data 16 bits") mtype=application/x-compress ;; *': '*'Zip archive data'*) mtype=application/zip ;; *': '*'ZIP archive') mtype=application/zip ;; *': '*'tar archive'*) mtype=application/x-tar ;; *': '*'RAR archive data'*) mtype=application/x-rar ;; *': '*'ASCII cpio archive'*) mtype=application/x-cpio-c ;; *': '*'Ogg-Vorbis compressed sound file'*) mtype=application/x-ogg ;; *': '*'MPEG system stream data'*) mtype=video/mpeg ;; *': '*'Microsoft Office Document'*) case "$file" in *.xls) mtype=application/msexcel ;; *) mtype=application/msword ;; esac ;; *': '*'RIFF (little-endian) data, AVI') mtype=video/avi ;; *': uuencoded or xxencoded text'*) mtype=application/x-uuencode ;; *': '*'PGP armored data signed message'*) mtype=text/plain ;; *': '*'ASCII '*) mtype=text/plain ;; *': '*'script'*) mtype=text/plain ;; *': '*'text'*) mtype=text/plain ;; *) mtype=`ext2mime "$file"` || mtype= ;; esac # unrecognised - just recite "file" output and fail [ -z "$mtype" ] && { echo "$type"; exit 1; } echo "$mtype" exit 0