/*
 * sfcomment.c--add comments to soundfiles
 *	stp - 4/92 based on cmix's sfhedit
 *
 *	usage: sfcomment "comment string" soundfile_name
 */

#include "../cmix/H/sfheader.h"
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <math.h>

static SFCODE	ampcode = {
	SF_MAXAMP, sizeof(SFMAXAMP) + sizeof(SFCODE)
}; 

static SFCODE	commentcode = {
	SF_COMMENT, MINCOMM + sizeof(SFCODE)
};

main(argc,argv) int argc; char *argv[]; {
	int sf, result, i, n, tfd, tn, nchars=MINCOMM;
	struct stat sfst;
	SFHEADER sfh;
	SFMAXAMP sfm;
	SFCOMMENT sfcm;
	SFCODE *sizer;
	FILE *fcom;
	char *comment, *sfname,*cp, *getsfcode();

	if(argc != 3) {
		printf("usage: sfcomment \"comment text\" soundfile_name\n");
		exit(-1);
	}
	comment = argv[1];
	sfname = argv[2];
	rwopensf(sfname, sf, sfh, sfst, "sfcomment", result, 2);
	if(result < 0)
		exit(1);
	cp = getsfcode(&sfh, SF_COMMENT);
	if (strlen(comment) > MAXCOMM) {
		printf("comment truncated to %d characters\n",MAXCOMM);
		comment[MAXCOMM] = '\0';
	}
	i = strlen(cp);
	strcpy(&(sfcomm(&sfcm, i)), comment);
	i = strlen(sfcm.comment);
/*
	if (i > MINCOMM)
		commentcode.bsize = i + sizeof(SFCODE);
	else
		commentcode.bsize = MINCOMM + sizeof(SFCODE);
*/
		commentcode.bsize = MAXCOMM + sizeof(SFCODE);
	result = putsfcode(&sfh, &sfcm, &commentcode);
	if(result < 0) {
		printf("comment didn't get written, sorry (%d)...\n", result);
		exit(-1);
	}
	lseek(sf, 0, 0);
	if(wheader(sf, (char *)&sfh)) {
	       printf("Can't seem to write header on file %s\n",sfname);
		perror("main");
		exit(-1);
	}
}
