<?xl version="0.1" encoding="utf-8" ?>


<Define> ^topnm-env (NewEnvironment ()) </Define>

<SetEA> ^topnm-env .jpg jpegtopnm </SetEA>
<SetEA> ^topnm-env .png pngtopnm </SetEA>
<SetEA> ^topnm-env .tif tifftopnm </SetEA>

<Define> ^netpbm ()
<Arguments>
	^src
	^dest
</Arguments>
<Sequence> ()
	<Define> ^prefix ^"" </Define>
	<RegSwitch> ^src
	<Case rex="\.[a-zA-Z_0-9]+">
		<set> ^prefix ^pattern </set>
	</Case>
	<Case rex=".">
		1
	</Case>
	</RegSwitch>
	<Define> ^prefix ([Cast type="string" option="small"] prefix) </Define>
	<ErrHandler>
	<Shell pipe="on" cmdout="on">
		(+ (EA topnm-env prefix)
		" " src " > " dest)
	</Shell>
	</ErrHandler>
	<Define> ^p (Parse
		([Shell pipe="on" cmdout="on"]
			(+ "ppm2r64 -ppmtype " dest)))
	</Define>
	<If> (= (GE p 0) "P4") <Then>
		<Shell pipe="on" cmdout="on">
			(+ "mv " dest " " dest ".pbm")
		</Shell>
		<Shell pipe="on" cmdout="on">
			(+ "pbmtopgm 3 3 " dest ".pbm > " dest ".pgm")
		</Shell>
		<Shell pipe="on" cmdout="on">
			(+ "pgmtoppm rgb:ff/ff/ff " dest ".pgm > " dest)
		</Shell>
		<Shell pipe="on" cmdout="on">
			(+ "rm " dest ".pbm")
		</Shell>
		<Shell pipe="on" cmdout="on">
			(+ "rm " dest ".pgm")
		</Shell>
	</Then></If>
	0
</Sequence>
</Define>
