/****************************************************************/
/*	psp			06.05.2006			*/
/****************************************************************/
/*	Short Description :					*/
/*	P[USH/OP]SPECTRUM					*/
/*	Program to manage the stack of spectra: save current 	*/
/*	spectrum to stack, open saved spectrum from the stack	*/
/****************************************************************/
/*	Keywords :						*/
/*	DIR, SEARCH						*/
/****************************************************************/
/*	Description/Usage :					*/
/*	Program to manage the stack of spectra: save current 	*/
/*	spectrum to stack, open any spectrum from the stack	*/
/*	Usage: 'psp' saves currently open spectrum (user, name,	*/
/*	expno, procno) to a stack file. Then, 'psp n' can be	*/
/*	used to get back to this spectrum (n is the number of	*/
/*	spectrum from top of the stack) or 'psp *' to select	*/
/*	saved spectrum from menu.				*/
/****************************************************************/
/*	Author(s) :						*/
/*	Name		: Arseny Slobodyuk			*/
/*	Organisation	: Institute of Chemistry, FEBRAS 	*/
/*	Email		: ampy at ich.dvo.ru       		*/
/****************************************************************/
/*	Name		Date	Modification:			*/
/****************************************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utime.h>

int	ftmod, td, shft, parmod, mc2, sizeofint, size;
const int maxmenu=128;
char	imag[4], filename[255], bfilename[255], *pmenu[maxmenu], menu[maxmenu*64];
struct  stat astat;
struct  utimbuf timbuf;
const int bufsz = 1024;
char buf[bufsz];
float   dw;
int     i, lines = 0, aexists=0, popl = 0, showmenu=0, mfreep, pfreep=0;
FILE *fin = NULL, *fout;

GETCURDATA

showmenu = (*cmd == '*' && cmd[1] == 0)?1:0;
if (showmenu) popl = 0; else popl = atol(cmd);

(void)sprintf(filename,"%s/data/expstack",disk);
(void)sprintf(bfilename,"%s/data/expstack.bak",disk);
memset(pmenu,0,sizeof(pmenu));


if (_stat(filename,&astat) == 0) aexists = 1;

if (aexists) {
  unlink(bfilename);
  if (rename(filename,bfilename)) {
    Show_status("Unable to backup expstack");
    goto L_end;
  }
}

fout = fopen(filename,"wt");
if (!fout) {
    Show_status("Unable to open result file");
    goto L_end;
}

if (aexists) {
  fin = fopen(bfilename,"rt");
  if (!fin) {
      Show_status("Unable to open backup file");
      fclose(fout);
      goto L_end;
  }
}

if (!popl && !showmenu)
  fprintf(fout,"%s\t%s\t%d\t%d\n",user,name,expno,procno);

if (fin) {
  while (fgets(buf,bufsz,fin)) {
    lines++;
    if (popl == lines) {
      int aexpno, aprocno;
      char auser[64], aname[128];
      if (sscanf(buf,"%s\t%s\t%d\t%d",auser,aname,&aexpno,&aprocno) == 4) {
        expno  = aexpno;
        procno = aprocno;
        strcpy(user,auser);strcpy(name,aname);
        VIEWDATA
        popl |= 0x100000;
      } else popl |= 0x200000;
    } else fputs(buf,fout);
    if (showmenu) {
      int len = strlen(buf);
      if (mfreep + len + 1 < sizeof(menu)) {
        pmenu[pfreep++] = menu+mfreep;
        strcpy(menu+mfreep,buf);
        mfreep+=len+1;
      }
    }
  }
  fclose(fin);
}

unlink(bfilename);

fclose(fout);

if (showmenu) {
  char * hires = uxselect("Saved spectra",pmenu,"Select experiment",SEL_RD_WIDE);
  if (hires) {
    for (i = 0; i < pfreep; i++) {
      if (strcmp(pmenu[i], hires) == 0) break;
    }
    sprintf(buf,"psp %d",i+1);
    XCMD(buf);
    goto L_end;
  }
}

if (popl & 0x200000)
  sprintf(buf,"Unable to sscanf line %d",popl & 0xFFFFF);
else if (popl & 0x100000)
  sprintf(buf,"Saved spectrum no %d retrieved",popl & 0xFFFFF);
else if (popl)
  sprintf(buf,"There's no spectrum no %d",popl);
else if (!showmenu)
  sprintf(buf,"Spectrum is saved to stack of %d spectra",lines+1);

Show_status(buf);
L_end:;
QUIT

