/* permuted title index */

char	*tfil "/tmp/p.tmp";
int	status;
char	*sw[] {
	"a",
	"an",
	"and",
	"as",
	"for",
	"is",
	"of",
	"on",
	"or",
	"the",
	"to",
	"up",
	"all",
	"at",
	"data",
	"do",
	"in",
	"into",
	"when",
	0};
char	line[200];
int	ch;
int	ptflg;
int	llen	72;

main(argc, argv)
int argc;
char *argv[];
{
	extern fin, fout;
	extern onintr();
	int f;

	if(argc>1 && *argv[1]=='-') {
		llen = 100;
		ptflg++;
		argc--;
		argv++;
	}
	if(argc<2) {
		write(2,"arg count\n",10);
		exit(0);
	}
	fin = open(argv[1]);
	if(fin < 0) {
		write(2,"cannot open input\n",18);
		exit(0);
	}
	f = creat(tfil, 0600);
	if(f < 0) {
		write(2,"cannot create in /tmp\n",22);
		exit(0);
	}
	fout = f;
	if ((signal(2, 1) & 01) ==0)
		signal(2, onintr);
	pass1();
	flush();
	close(fin);
	fin = 0;
	close(fout);
	fout = 1;
	f = fork();
	if(f < 0) {
		write(2,"try again\n",10);
		exit(0);
	}
	if(f == 0) {
		execl("/bin/sort", "sort", "-df", "-o", tfil, tfil, 0);
		execl("/usr/bin/sort", "sort", "-df", "-o", tfil, tfil, 0);
		write(2,"someone moved sort\n",19);
		exit(0);
	}
	while(wait(&status) != f);
	fin = open(tfil, 0);
	if(fin < 0) {
		write(2,"cannot reopen tmp file\n",23);
		exit(0);
	}
	if (argc>=3)
		f = creat(argv[2], 0666);
	else
		f = dup(1);
	if(f < 0) {
		write(2,"cannot create output\n",21);
		exit(0);
	}
	fout = f;
	pass2();
	flush();
	onintr();
}

onintr()
{
	unlink(tfil);
	exit(0);
}

pass1()
{
	int n, c, i, ll, j, k, cc, ccc;

loop:
	if ((c=getchar())=='\0')
		return;
	n = 0;
	i = 0;
	while(c!='\n' && c!='\0') {
		if(c == '(')
			c = 0177;
		if(c==' ' || c=='\t') {
			i++;
			c = getchar();
			continue;
		}
		if(i) {
			i = 0;
			if(n<=llen) line[n++] = ' ';
		}
		if (n<=llen) line[n++] = c;
		c = getchar();
	}
	ll = n;
	line[n++] = 0;
	i = -1;
l1:
	while((cc=line[++i])==' ');
	n = i;
	j = 0;
	while(sw[j]) {
		i = n;
		k = 0;
		while ((cc=sw[j][k++])==line[i++]);
		if(cc==0 && ((ccc=line[--i])==' '||ccc==0))
			goto l1;
		j++;
	}
	i = n;
	while (c=line[n++]) putchar(c);
	putchar('~');
	n = 0;
	while (n<i) {
		c = line[n++];
		if (c!=' ' || n!=i)
			putchar(c);
	}
	putchar('\n');
	while((c=line[i++])!=0 && c!=' ');
	--i;
	if (c) goto l1;
	goto loop;
}

pass2()
{
	int i, n, c, tilde, llen2, nbfore, nafter;


	llen2 = llen/2+6;
loop:
	if ((c=getchar())=='\0')
		return;
	n = nbfore = nafter = 0;
	tilde = -1;
	while(c!='\n' && c!='\0') {
		if(c == 0177)
			c = '(';
		if (n<=llen) line[n] = c;
		if (c=='~') tilde = n;
		if (tilde>=0) nafter++; else nbfore++;
		n++;
		c = getchar();
	}
	if (tilde<0)
		tilde = n++;
	nafter--;
	if (nbfore>llen2) {
		i = tilde;
		while (nbfore > llen2)
			while(line[--i]!=' ' && i>=0) nbfore--;
		if (i<0) goto l1;
		line[tilde] = 0200;
		nafter =+ (tilde-i+2);
		tilde = i;
	}
	if (nafter >= llen-llen2) {
		i = tilde;
		while(nafter-- >= llen-llen2)
			while(line[++i]!=' ' && i<n) nafter--;
		if (i>=n) goto l1;
		line[tilde] = 0200;
		nafter++;
		tilde = i;
	}
l1:
	if(!ptflg) {
		for(i=llen-llen2-nafter; i>=8; i =- 8)
			putchar('\t');
		while(--i>=0)
			putchar(' ');
	} else
		printf(".xx \"");
	i = tilde;
	while (++i<n) p1char(line[i]);
	if(!ptflg)
		printf("  "); else
		printf("\" \"");
	i = -1;
	while(++i<tilde) p1char(line[i]);
	if(ptflg)
		putchar('"');
	putchar('\n');
	goto loop;
}

p1char(c)
{
	if ((c&0377) == 0200) {
		putchar('.');
		putchar('.');
		c = '.';
	}
	putchar(c);
}
