#!/bin/sh
#
#     mutt.octet.filter - Octet filter for use with the mutt autoview facility
#     Copyright (C) 1997 David A Pearson
#   
#     This program 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 2 of the license, or 
#     (at your option) any later version.
#     
#     This program 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 this program; if not, write to the Free Software
#     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

#
# This script file is a pretty brain-dead, last resort, "works for me"
# utility that will attempt to make sense of any octet-stream data
# that is received as part of an email and act as a filter for use
# with mutt's auto_view ability.
#
# Here is how I use it. In my ~/.mutt_mailcap (use your filename of
# choice) I have the following entry:
#
# application/octet-stream; mutt.octet.filter %s | vcat -s; copiousoutput
#
# 'vcat' is a silly little utility that strips any non-printing
# characters, use your non-printing filter of choice. You don't need
# that filter there, mutt.octet.filter attempts to not show binary
# data, but belt and braces can be a good idea now and again.
#
# All you then need to do is add a line like:
#
# auto_view application/octet-stream 
#
# to your ~/.muttrc (use your filename of choice).
#
# In it's current state the script isn't perfect, it's your typical
# "works for me" type of script and is offered in the hope that it
# maybe handy and that you can do something with it.
#
# All comments/flames/feedback can be directed to:
#
#               davep@hagbard.demon.co.uk
#

function ShowTAR()
{
    tar tvvf "$1" 2> /dev/null
}

function ShowTGZ()
{
    tar tzvvf "$1" 2> /dev/null
}

function ShowGZIP()
{
    gzip -dc "$1" 2> /dev/null
}

function ShowZIP()
{
    unzip -l "$1" 2> /dev/null
}

function ShowARJ()
{
    unarj l "$1" 2> /dev/null
}

function ShowEXE()
{
    echo $(basename "$1"): DOS/Windows executable
}

function Showdata()
{
    echo $(basename "$1"): unprintable data
#    cat "$1"
}

function DisplayFileType()
{
    echo "[-- $(basename $0) file type: \"$1\" --]"
    echo
}

function ShowFileType()
{
    FILE_TYPE=$(echo $(file "$1" 2> /dev/null) | cut -d' ' -f 2-)
    DisplayFileType "$FILE_TYPE"
}

function ShowMISC()
{
    FILE_TYPE=$(file -z "$1" 2> /dev/null)

    if [ $? -gt 0 ]
    then
	FILE_TYPE=$(file "$1" 2> /dev/null)
    fi

    FILE_TYPE=$(echo "$FILE_TYPE" | cut -d' ' -f 2-)

    DisplayFileType "$FILE_TYPE"

    case "$FILE_TYPE" in
	*tar*archive*gzip* ) ShowTGZ  "$1";;
	*tar*archive*      ) ShowTAR  "$1";;
	*gzip*             ) ShowGZIP "$1";;
	*ARJ*archive*data* ) ShowARJ  "$1.";; # "." gets round bug in unarj.
	*zip*archive*file* ) ShowZIP  "$1";;
	*DOS*executable*   ) ShowEXE  "$1";;
	*ascii*text*       ) cat      "$1";;
	*c*program*text*   ) cat      "$1";;
	data               ) Showdata "$1";;
	*                  ) cat      "$1";;
    esac
}

if [ "$1" = "" ]
then
    echo "syntax: $(basename '$0') file"
else
    case "$1" in
	*.tar )    ShowFileType "$1"; ShowTAR  "$1";;
	*.tgz )    ShowFileType "$1"; ShowTGZ  "$1";;
	*.tar.gz ) ShowFileType "$1"; ShowTGZ  "$1";;
	*.tar.Z )  ShowFileType "$1"; ShowTGZ  "$1";;
	*.tar.z )  ShowFileType "$1"; ShowTGZ  "$1";;
	*.Z )      ShowFileType "$1"; ShowGZIP "$1";;
	*.z )      ShowFileType "$1"; ShowGZIP "$1";;
	*.gz )     ShowFileType "$1"; ShowGZIP "$1";;
	*.zip )    ShowFileType "$1"; ShowZIP  "$1";;
	*.ZIP )    ShowFileType "$1"; ShowZIP  "$1";;
	*.arj )    ShowFileType "$1"; ShowARJ  "$1";;
	*.ARJ )    ShowFileType "$1"; ShowARJ  "$1";;
	*.log )    ShowFileType "$1"; cat "$1";;
	*.LOG )    ShowFileType "$1"; cat "$1";;
	* )        ShowMISC "$1";;
    esac
fi

