# include "../sccshead/sfile.h"
# include "../sccshead/sint.h"
# include "../sccshead/statbuf.h"


char rmdel_[] "~|^`rmdel.c	3.5";

struct Packet gpkt;
int hadr;
int	num_files;
int	release;
int	level;
struct Statbuf sb;

main(argc,argv)
int argc;
char *argv[];
{
	register int i;
	register char *p;
	char c;
	extern rmdel();

	for(i=1; i<argc; i++)
		if(argv[i][0] == '-' && (c=argv[i][1])) {
			p = &argv[i][2];
			switch (c) {

			case 'r':
				dor(&release,&level,p);
				break;
			default:
				fatal("unknown key letter (69)");
			}

			if (hadr++)
				fatal("key letter twice (464)");
			argv[i] = 0;
		}
		else num_files++;

	if(num_files == 0) fatal("missing file arg (49)");
	setsig(fatal,fatal,fatal);
	for (i=1; i<argc; i++)
		if (p=argv[i])
			do_file(p,rmdel);
	exit(0);
}


rmdel(rfile)
char *rfile;
{
	struct Packet *pkt;
	register int i;
	register char *file;

	sinit(&gpkt,rfile,1);

	gpkt.Prel = release;
	gpkt.Plev = level;
	pkt = &gpkt;
	if (hadr==0) fatal("missing r (410)");
	if (pkt->Plev== -1) fatal("no level specified (411)");
	pkt->Pupd = 1;
	dohead(pkt);
	dorelt(pkt);
	dodelt(pkt);
	dobody(pkt);
	wrtrec(pkt,0,-1);
	rename(auxf(&gpkt,'x'),gpkt.Pfile);
	xrm(pkt);
}


dorelt(pkt)
struct Packet *pkt;
{
	register struct Reltab *rt;

	while ((rt=getrec(pkt))->Rrel)
		if (rt->Rrel==pkt->Prel && --(rt->Rlevs)==0) pkt->Pwrttn = 1;
}


dodelt(pkt)
struct Packet *pkt;
{
	register struct Deltab *dt;
	register int didrm;
	register char type;

	didrm = 0;
	while ((dt=getrec(pkt))->Drel) {
		if (!didrm && ((type=dt->Dtype)=='D' ||
			type=='P' ||
			type=='U' ||
			type=='I' ||
			type=='E')) {
				didrm = 1;
				dt->Dtype = 'R';
				rmpermiss(pkt,dt);
		}
	}
	if (!didrm)
		fatal("no delta (310)");
}


rmpermiss(pkt,dt)
register struct Packet *pkt;
register struct Deltab *dt;
{
	char lname[SZLNAM], *username, *pfile;
	int r, fowner, downer, user;

	if (exists(pfile = auxf(pkt,'p'))) {
		rdpfile(pfile,&r,lname);
		if (!equal(lname,username = getlnam()))
			fatal("you are not `",lname,"' (307)");
		if (!equal(dt->Dpgmr,username))
			fatal("you are not `",dt->Dpgmr,"' (308)");
	}
	else {
		fstat(pkt->Pibuf.Ifildes,&sb);
		fowner = sb.uid&0377;
		if (stat(dname(pkt->Pfile),&sb))
			downer = -1;
		else
			downer = sb.uid&0377;
		user = getuid()&0377;
		if (user != fowner || user != downer)
			fatal("sorry (309)");
	}
	if (dt->Drel!=pkt->Prel || dt->Dlev!=pkt->Plev)
		fatal("not the most recent delta (416)");
}


dobody(pkt)
register struct Packet *pkt;
{
	register struct Control *ctl;
	register int keep;
	int rel, lev;

	rel = pkt->Prel;
	lev = pkt->Plev;
	keep = YES;
	while (1) {
		ctl = getrec(pkt);
		if (ctlrec(ctl,pkt->Pibuf.Ilen)) {
			if (ctl->Crel==rel && ctl->Clev==lev) {
				pkt->Pwrttn = 1;
				if (ctl->Cctl==INS) keep=NO;
				else keep=YES;
			}
			else if (ctl->Crel==0 && ctl->Clev==0) return;
		}
		else if (keep==NO) pkt->Pwrttn = 1;
	}
}


getrec(pkt)
struct Packet *pkt;
{
	if (rdrec(pkt)==1) fatal("premature eof (58)");
	return(pkt->Pibuf.Irecptr);
}
