#!/usr/local/plan9/bin/rc

# extract pages $OLIST from postscript on stdin

if(~ $OLIST '')
	exec cat

awk '
BEGIN {
	x=ENVIRON["OLIST"];
	gsub(/^-o/, "", x);
	na = split(x, a, ",");
	header = 1;
	goodpage = 0;
}

header || goodpage {
 	print
}

/^%%EndSetup[ 	]*$/ { header = 0; next }

/^%%Page:/ {
	header = 0;
	p=$2+0;
	goodpage = 0;
	for(i=1; i<=na; i++){
		if(aa=match(a[i], "-")){
			low=substr(a[i], 1, RSTART);
			high=substr(a[i], RSTART+RLENGTH);
			if(low == "")
				low = 0;
			else
				low = low+0;
			if(high == "")
				high = 100000;
			else
				high = high+0;
			if(low <= p && p <= high){
				goodpage = 1;
				break;
			}
		}else{
			if(a[i] == p){
				goodpage = 1;
				break;
			}
		}
	}
}

/^%%EndPage[ 	]*$/ {
	goodpage = 0;
}

/^%%Trailer[ 	]*$/ { 
	goodpage = 1;
}

'
