/* **************************************************************
   *								*
   *                    STP Encoder Program                     *
   *								*
   *  Original version by: Mnp (unknown)                        *
   *  Amiga (optimized) version by: Manolis S Pappas            *
   *                                Thermopilon 24              *
   *				    14231 Nea Ionia		*
   *				    Athens GREECE		*
   *								*
   *  E-mail:							*
   *		mpappas@posidon.servicenet.ariadne-t.gr 	*
   *            mpappas@acropolis.net                           *
   *		2:410/128.19					*
   *		39:250/3.19					*
   **************************************************************
*/
/* Version 1.6c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>        /* WARNING: Needed for SPECIFIC SAS/C function */

#define false(a) (((a)<32)||(((a)>123) && ((a)<179))||((a)>216))
#define R_MARGIN 65

#define VERSION "1.6c"
static const char CLI_VERS[]="$VER: AmySTP Encoder v1.6c "__AMIGADATE__;

 int err(int e)
 {
    char *errlist[12];

    errlist[0]="Cannot open input file";
    errlist[1]="Cannot create output file";
    errlist[3]="Cannot open input file";
    errlist[7]="Cannot create output file";
    errlist[9]="Internal error";

   fprintf("Error(%d): %s\n",e,errlist[e-2]);
   return e;
 }


 main(int argc,char *argv[])
 {
     FILE *fin, *fout;
     unsigned char z=R_MARGIN,x,n=7,i,j,a,b[7],c[8],terminate=0;
     unsigned int part=1,sum=1,l;
     unsigned long checksum=0,fsize=0,chars=0,newsum=0;
     char s[80]="";
     char *string[5], *point=".";
     char drive[FNSIZE],path[FMSIZE],node[FNSIZE],ext[FESIZE]; /* WARNING: SAS/C specific function's variables */
   /* NOTE: Fxxxx variables are TOTALLY AMIGA SPECIFIC */
           
 string[0]="START STP SCRIPT %u OF %u\n";
 string[1]="FILENAME : %s - FILESIZE : %lu\n";
 string[2]="CHARACTERS : %lu - CHECKSUM : %lX\n";
 string[3]="\nEND STP SCRIPT\n";
 string[4]="Successful. Checksum :";

 if (argc<3)
    {
           fprintf(stderr,"\nAmySTP Binary Mail Encoder "__AMIGADATE__
                  ".\nCopyright © 1994-97, Infinity Labs Development. All Rights Reserved.\n"
                  "Author: Manolis S Pappas. Version: %s\n",VERSION);
           fprintf(stderr,"\nUsage: %s [binary input file] [output text file] {-d=[right margin]}\n",argv[0]);
   
   exit(1);
    }

   if ((fin=fopen(argv[1],"rb"))==NULL) exit(err(2));

   if ((fout=fopen(argv[2],"w"))==NULL) exit(err(3));

    if (argc>3)
    {
        
	for (i=3;i<argc;i++)
        {
            if (strstr(argv[i],"-d="))
            {
                z=(char)atoi(argv[i]+=3);
                if ((z<10)||(z>78))
                {
                    fprintf(stderr,"Right margin must be in the range of 10..78.\n"
                                   "Using default right margin (65) ...\n");
                    z=R_MARGIN;
                }
                else fprintf(stderr,"Right margin will be : %u\n",z);
            }
        }
        for (i=3;i<argc;i++)
        {
            /*
            if (strstr(argv[i],"-l="))
            {
                l=atoi(argv[i]+=3);
                fprintf(stderr,"Suggested output file length : %u\n",l);
		}
             */
        }
    }

    x=z-1;
    a=getc(fin);
    while(!feof(fin))
    {
        fsize++;
        checksum+=a;
        a=getc(fin);
    }
    chars=(fsize/7)*8+(fsize%7)+1*(fsize%7!=0);
    
    strsfn(argv[1],drive,path,node,ext); /* WARNING: SAS/C specific function: split complete pathargument into portions (name,drive,path,node,ext) */
    
    if((stcgfe(ext,argv[1]))!=0){ /* WARNING: Another SAS/C specific function: stcgfe */
        strcat(node, point);
        strcat(node, ext);
    }
    fprintf(fout,string[0],part,sum);
    fprintf(fout,string[1],node,fsize);
    fprintf(fout,string[2],chars,checksum);

    rewind(fin);
    b[0]=getc(fin);
    while (!feof(fin))
   {
        for (i=1;i<7;i++)
        {
            b[i]=getc(fin);
            if (feof(fin)) b[i]=0;
        }
                c[0]=(b[0]>>1);
                c[1]=((b[0]& 1)<<6)|(b[1]>>2);
                c[2]=((b[1]& 3)<<5)|(b[2]>>3);
                c[3]=((b[2]& 7)<<4)|(b[3]>>4);
                c[4]=((b[3]&15)<<3)|(b[4]>>5);
                c[5]=((b[4]&31)<<2)|(b[5]>>6);
                c[6]=((b[5]&63)<<1)|(b[6]>>7);
                c[7]=(b[6]&127);
        for (i=0;i<8;i++)
        {
            putc(c[i]<91?33+c[i]:89+c[i],fout);
            if (!(--chars)) break;
            if (!(x--||feof(fin))) { fprintf(fout,"\n");x=z-1; }
        }
        b[0]=getc(fin);
    }
    fprintf(fout,string[3]);
    fclose(fin); fclose(fout);
    fprintf(stderr,"File encoding %s %lX\n", string[4], checksum);
    return 0;
 }
