summaryrefslogtreecommitdiffstats
path: root/tdeprint/tdeprintfax/anytops
blob: 9718831dcb698c36dab924d89805fccedd2bfc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh

FAXFILTERS=./faxfilters
MIMETYPE=
PAGE="letter"

usage() {
	echo "Usage: anytops [-m <filters>] [--mime=<mime-type>] [-p <page>] [--help] <infile> <outfile>"
	exit $1
}

while true; do
	case "$1" in
		--help)
			usage 0
			;;
		-m)
			shift
			FAXFILTERS="$1"
			shift
			;;
		--mime=*)
			MIMETYPE=${1#--mime=}
			shift
			;;
		-p)
			shift
			PAGE="$1"
			shift
			;;
		-v)
			echo "Anytops v1.0: (c) 2001, Michael Goffioul <tdeprint@swing.be>";
			echo "Small script to convert any file type into PS. Part of tdeprintfax."
			exit 0
			;;
		*)
			break;
			;;
	esac
done

INFILE=$1
OUTFILE=$2

if [ -z "$INFILE" -o -z "$OUTFILE" -o ! -f "$INFILE" ]; then
	usage 1;
fi

if [ -z "$MIMETYPE" ]; then
	MAGIC=$KDEDIR
	if [ -z "$MAGIC" ]; then
		MAGIC=/usr
	fi
	MAGIC=$MAGIC/share/mimelnk/magic

	if [ ! -f "$MAGIC" ]; then
		echo "KDE magic file not found. Check your installation."
		exit 1;
	fi

	MIMETYPE=`file -b -m $MAGIC "$INFILE" 2> /dev/null`
	MIMETYPE=${MIMETYPE%%;*}
fi

echo "Mime type: $MIMETYPE"
MIMETYPE_BASE=`echo $MIMETYPE | cut -f 1 -d "/"`
MIMETYPE_TYPE=`echo $MIMETYPE | cut -f 2- -d "/"`

get_command() {
	while read mime command; do
		mimebase=`echo $mime | cut -f 1 -d "/"`
		mimefile=`echo $mime | cut -f 2- -d "/"`
		if test "$mimebase" = "$MIMETYPE_BASE" || test "$mimebase" = "*"; then
			if test "$mimefile" = "$MIMETYPE_TYPE" || test "$mimefile" = "*"; then
				echo $command
				break;
			fi
		fi
#		PATTERN=`echo $MIMETYPE | grep $mime`
#		if [ "$PATTERN" = "$MIMETYPE" ]; then
#			echo $command
#			break;
#		fi
	done
}

COMMAND=`cat $FAXFILTERS | get_command`

if [ -z "$COMMAND" ]; then
	echo "$MIMETYPE: file type not handled"
	exit 1;
fi

echo "Command: $COMMAND"

PAGEUP="`echo $PAGE | cut -c 1 | tr [:lower:] [:upper:]``echo $PAGE | cut -c 2-`"
EXECCOMMAND=`echo $COMMAND | sed -e "s,%in,\"$INFILE\"," -e "s,%out,\"$OUTFILE\"," -e "s,%psl,$PAGE," -e "s,%psu,$PAGEUP,"`
eval "$EXECCOMMAND"