#include <stdio.h>         /* includes */
#include <string.h>
#include <dos/dos.h>
#include <dos/rdargs.h>

#define DEBUG            0     /* set to 1 for debugging */

#define aFILE            0     /* defines for ReadArgs */
#define aOUTPUTFILE      1
#define aPATH            2
#define aSECONDS         3
#define aTABLEONLY       4
#define aNOCOLOR         5
#define aBGCOLOR         6
#define aTEXTCOLOR       7
#define aLINKCOLOR       8
#define aVLINKCOLOR      9
#define aALINKCOLOR     10
#define aEVENCOLOR      11
#define aODDCOLOR       12
#define aCOUNTER        13
#define TEMPLATE "FILE/A,OUTFILE/K,PATH/S,SECONDS/S,TABLEONLY/S,NOCOLOR/S,BGCOLOR/K,TEXTCOLOR/K,LINKCOLOR/K,VLINKCOLOR/K,ALINKCOLOR/K,EVENCOLOR/K,ODDCOLOR/K"

const UBYTE *vers="$VER: Aamp2HtmlConvert-r 1.2";

struct RDArgs *ReadArgs();   /* protos */
void FreeArgs();
long atol();

int main(int argc, char *argv[])
{
    FILE *in=0, *out=0;
    struct RDArgs *rdargs=0;
    short errors=0, tableonly=0, linkcolorcheck=0, vlinkcolorcheck=0, alinkcolorcheck=0, songcounter=0, idx=0, field=0, days=0, hours=0, minutes=0, seconds=0, average=0, averageminutes=0, averageseconds=0, secondsoutput=0, path=0, color=1;
    char check=0, check2=0, timestring[6], bgcolor[7]="FFFFFF", textcolor[7]="000000", linkcolor[7]="0000FF", vlinkcolor[7]="5555FF", alinkcolor[7]="9999FF", evencolor[7]="00FFFF", oddcolor[7]="FFFFFF";
    long time=0, allsongs=0, templatearray[aCOUNTER]={0,0,0,0,0,0,0,0,0,0,0,0,0};
    printf("|     Aamp2HtmlConvert-r  1.2    |\n|  by radiat-r aka Sascha Ploss  |\n| www.Global-Multimedia.de/amiga |\n");
    if (argc == 1)
    {
        printf("Error, no file specified.\n1 error occured.\n");
        return 1;
    }
    rdargs=(struct RDArgs *) ReadArgs(TEMPLATE, templatearray, NULL);
    if (rdargs)
    {
        if (templatearray[aSECONDS])
        {
            secondsoutput=1;
        }
        if (templatearray[aNOCOLOR])
        {
            color=0;
        }
        if (templatearray[aTABLEONLY])
        {
            tableonly=1;
        }
        if (templatearray[aBGCOLOR])
        {
            strncpy(bgcolor,(char *) templatearray[aBGCOLOR],6);
            bgcolor[6]='\0';
        }
        if (templatearray[aTEXTCOLOR])
        {
            strncpy(textcolor,(char *) templatearray[aTEXTCOLOR],6);
            textcolor[6]='\0';
        }
        if (templatearray[aLINKCOLOR])
        {
            strncpy(linkcolor,(char *) templatearray[aLINKCOLOR],6);
            linkcolor[6]='\0';
            linkcolorcheck=1;
        }
        if (templatearray[aVLINKCOLOR])
        {
            strncpy(vlinkcolor,(char *) templatearray[aVLINKCOLOR],6);
            vlinkcolor[6]='\0';
            vlinkcolorcheck=1;
        }
        if (templatearray[aALINKCOLOR])
        {
            strncpy(alinkcolor,(char *) templatearray[aALINKCOLOR],6);
            alinkcolor[6]='\0';
            alinkcolorcheck=1;
        }
        if (templatearray[aEVENCOLOR])
        {
            strncpy(evencolor,(char *) templatearray[aEVENCOLOR],6);
            evencolor[6]='\0';
        }
        if (templatearray[aODDCOLOR])
        {
            strncpy(oddcolor,(char *) templatearray[aODDCOLOR],6);
            oddcolor[6]='\0';
        }
        if (templatearray[aPATH])
        {
            path=1;
        }
        in=(FILE *) fopen((char *) templatearray[aFILE], "rb");
        if (in)
        {
            check=fgetc(in);
            if (check != EOF)
            {
                fseek(in,ftell(in)-1,SEEK_SET); /* move back to beginning of file */
                if (templatearray[aOUTPUTFILE])
                {
                    out=(FILE *) fopen((char *) templatearray[aOUTPUTFILE], "wb");
                }
                else
                {
                    out=(FILE *) fopen("playlist.html", "wb");
                }
                if (out)
                {
                    if (!tableonly)
                    {
                        fprintf(out,"<HTML><HEAD><TITLE>AmigaAMP Playlist</TITLE></HEAD>\n<BODY BGCOLOR=\"#%s\" TEXT=\"#%s\"", bgcolor, textcolor);
                        if (linkcolorcheck) fprintf(out," LINK=\"#%s\"",linkcolor);
                        if (vlinkcolorcheck) fprintf(out," VLINK=\"#%s\"",vlinkcolor);
                        if (alinkcolorcheck) fprintf(out," ALINK=\"#%s\"",alinkcolor);
                        fprintf(out,">\nAmigaAMP Playlist created with <A HREF=\"http://amigaamp.amiga-software.com\">AmigaAMP</A> by Thomas Wenzel,<BR>\nconverted to HTML with <A HREF=\"http://www.Global-Multimedia.de/amiga\">Aamp2HtmlConvert-r</A> by radiat-r aka Sascha Ploss<HR>\n");
                    }
                    fprintf(out,"<TABLE>\n<TR><TD><B>Nr.</B></TD>");
                    if (path) fprintf(out,"<TD><B>Path</B></TD>");
                    fprintf(out,"<TD><B>Artist</B></TD><TD><B>-</B></TD><TD><B>Title</B></TD><TD><B>Length</B></TD></TR>\n");
                    songcounter=1;
                    do  /* move through whole file */
                    {
                        field=0; /* Artist, Title or Length */
                        check2=check;
                        check=fgetc(in);
                        if ((check != EOF) && (check2 != EOF))
                        {
                            if (color) /* colored backgrounds in list */
                            {
                                if (songcounter-(songcounter/2)*2) /* check if odd or even */
                                {
                                    fprintf(out,"<TR BGCOLOR=\"#%s\"><TD>%03hi</TD><TD>", oddcolor, songcounter); /* odd, init row with oddcolor and write songcounter to file */
                                }
                                else
                                {
                                    fprintf(out,"<TR BGCOLOR=\"#%s\"><TD>%03hi</TD><TD>", evencolor, songcounter); /* even, init row with evencolor and write songcounter to file */
                                }
                            }
                            else /* no colored backgrounds in list */
                            {
                                fprintf(out,"<TR><TD>%03hi</TD><TD>",songcounter); /* write songcounter to file */
                            }
                        }
                        while ((check != 0x09) && (check != 0x0A) && (check != EOF))
                        {
                            if (path) fprintf(out,"%c",check);
                            check=fgetc(in);
                        }
                        if (check == 0x09) /* search first tab, to remove path and filename */
                        {
                            field++; /* first field (artist) found */
                            songcounter++; /* increase songcounter */                            
                            if (path) fprintf(out,"</TD><TD>");
                            if (DEBUG) fprintf(out,"Field %hi: ",field); /* for debugging purose, check if were really in the right field */
                            check=fgetc(in);
                            while ((check != 0x0A) && (check != EOF))  /* loop for single song, ends either with end of line or end of file */
                            {
                                if (check == 0x09) /* found the next tab */
                                {                                    
                                    field++; /* and so this must be the next field */
                                    if (field == 2) /* check if were already in title field (it isnt separated with tabs, so it must be empty) */
                                    {
                                        fprintf(out,"</TD><TD>-</TD><TD>&nbsp;"); /* add separator for empty title field */
                                        if (DEBUG) fprintf(out,"Field %hi: ",field); /* debugging purpose again */
                                    }
                                    fprintf(out,"</TD><TD>"); /* end last field and init next one */
                                    if (DEBUG) fprintf(out,"Field %hi: ",field); /* debugging */
                                }
                                else if ((check == 0x20) && (field == 1))  /* blank in artist field (maybe end of artist and start of seperator) */
                                {
                                    check=fgetc(in);  /* get the next two chars... */
                                    check2=fgetc(in);
                                    if ((check == 0x2D) && (check2 == 0x20)) /* ...to check if we have the separator: " - " */
                                    {
                                        field++;
                                        fprintf(out,"</TD><TD>-</TD><TD>");  /* add the separator */
                                        if (DEBUG) fprintf(out,"Field %hi: ",field);
                                    }
                                    else /* not the separator, just a normal blank */
                                    {
                                        fseek(in,ftell(in)-2,SEEK_SET); /* move the two preread chars back */
                                        fprintf(out," "); /* write the read blank */
                                    }
                                }
                                else /* neither tab nor eol/eof found, so its a normal text char */
                                {
                                    if (field == 3) /* arrived in lenght field */
                                    {
                                        time=0;
                                        idx=0;
                                        do
                                        {
                                            if (idx<6) /* make sure were not writing outside of string */
                                            {
                                                if (check == 0x20) /* must be end of time,... */
                                                {
                                                    timestring[idx]='\0'; /* ...so end string */
                                                }
                                                else /* number */
                                                {
                                                    timestring[idx]=check;  /* save in string */
                                                }
                                            }
                                            idx++; /* stringposition */
                                            check=fgetc(in); /* next char */
                                        } while ((check != 0x0A) && (check != EOF)); /* move till end of timefield */
                                        fseek(in,ftell(in)-1,SEEK_SET); /* move back to end of line/file */
                                        time=atol(timestring); /* convert time string to long */
                                        if (DEBUG) printf("lenght: %li\n",time); /* say for debugging */
                                        allsongs=allsongs+time; /* add length of actual song to summary */
                                        if (secondsoutput)
                                        {
                                            fprintf(out,"%03li Seconds",time); /* write time to file */
                                        }
                                        else
                                        {
                                            minutes=time/60;
                                            seconds=time-minutes*60;
                                            fprintf(out,"%02hi:%02hi",minutes,seconds); /* write time to file */
                                        }
                                    }
                                    else /* not yet in third field */
                                    {
                                        fprintf(out,"%c",check); /* write the char to file */
                                    }
                                }
                                check=fgetc(in); /* move on and get the next char */
                            }
                            fprintf(out,"</TD></TR>\n"); /* end of line/file */
                        } /* endif for first tab check */
                    } while (check != EOF); /* loop till end of file */
                    fprintf(out,"</TABLE>\n"); /* finish table */
                    hours=allsongs/3600;        /* time calculations - seconds to (days:)hours:minutes:seconds */
                    if (hours > 24)
                    {
                        days=hours/24;
                        hours=hours-days*24;
                        minutes=(allsongs-(hours+days*24)*3600)/60;
                        seconds=(allsongs-(hours+days*24)*3600)-minutes*60;
                    }
                    else
                    {
                    minutes=(allsongs-hours*3600)/60;
                    seconds=(allsongs-hours*3600)-minutes*60;
                    }
                    average=allsongs/(songcounter-1); /* calculate average songlength */
                    averageminutes=average/60;
                    averageseconds=average-(averageminutes*60);
                    if (days)
                    {
                        printf("%hi files in playlist with a total length of %02hi days, %02hi hours, %02hi minutes and %02hi seconds (%li seconds).\nAverage songlength is %02hi:%02hi.\n",songcounter-1,days,hours,minutes,seconds,allsongs,averageminutes,averageseconds); /* were finished, say some results */
                        fprintf(out,"<HR>\n%hi files in playlist with a total length of %02hi days, %02hi hours, %02hi minutes and %02hi seconds (%li seconds).<BR>\nAverage songlength is %02hi:%02hi.<BR>\n",songcounter-1,days,hours,minutes,seconds,allsongs,averageminutes,averageseconds); /* save the same info to the file */
                    }
                    else
                    {
                        printf("%hi files in playlist with a total length of %02hi hours, %02hi minutes and %02hi seconds (%li seconds).\nAverage songlength is %02hi:%02hi.\n",songcounter-1,hours,minutes,seconds,allsongs,averageminutes,averageseconds); /* were finished, say some results */
                        fprintf(out,"<HR>\n%hi files in playlist with a total length of %02hi hours, %02hi minutes and %02hi seconds (%li seconds).<BR>\nAverage songlength is %02hi:%02hi.<BR>\n",songcounter-1,hours,minutes,seconds,allsongs,averageminutes,averageseconds); /* save the same info to the file */
                    }
                    if (!tableonly)
                    {
                        fprintf(out,"</BODY></HTML>"); /* finish the outputfile */
                    }
                    if (out) fclose(out);
                    out=0;
                }
                else
                {
                    printf("Error, could not open outfile.\n");
                    errors++;
                }
            }
            else
            {
                printf("Error, inputfile is empty.\n");
                errors++;
            }
            if (in) fclose(in);
            in=0;
        }
        else
        {
            printf("Error, could not open inputfile.\n");
            errors++;
        }
        if (rdargs) FreeArgs(rdargs);
        rdargs=0;
    }
    else
    {
        printf("Error, could not read args.\n");
        errors++;
    }
    if (errors == 1) printf("1 error occured.\n");
    if (errors > 1) printf("%hi errors occured.\n", errors);
    return errors;
}
