/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Internet Radio Automation & Encoding Toolkit
#+
#+     Copyright (C) 2018  - 2025 by Kevin C. O'Kane
#+
#+     Kevin C. O'Kane
#+     kc.okane@gmail.com
#+     https://www.cs.uni.edu/~okane
#+     https://threadsafebooks.com/
#+
#+ This program is free software; you can redistribute it and/or modify
#+ it under the terms of the GNU General Public License as published by
#+ the Free Software Foundation; either version 2 of the License, or
#+ (at your option) any later version.
#+
#+ This program is distributed in the hope that it will be useful,
#+ but WITHOUT ANY WARRANTY; without even the implied warranty of
#+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#+ GNU General Public License for more details.
#+
#+ You should have received a copy of the GNU General Public License
#+ along with this program; if not, write to the Free Software
#+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//	Sept 24, 2025

#include "includes.h"
#include "common-externs.h"

//----------------------------------------------------------------
//	load program.lst
//----------------------------------------------------------------

void	LoadProgram(char * file) {

	FILE *s1;
	char cmd[4096];
	int i;

	int Album = 0, Track = 0;

	for (int i = 0; i < PROGRAM_MAX; i++) {
		strcpy(ProgramArray[i], "");
		programData[i]->play = 0;
		programTime[i] = -1;
		}

//----------------------------------------------------------------
//	load program entries
//----------------------------------------------------------------

	if (file[0] != '\"') strcpy(cmd, file);
	else strcpy(cmd, &file[1]);

	i = strlen(cmd);
	if (cmd[i - 1] == '\"') cmd[i - 1] = 0;

	s1 = fopen(cmd,"r");

	if (s1==NULL) {		// no program.lst file
		SP->scount = 0;	// program list is empty
		printf("*** No program.lst file found\n");
		i = 0;
		while (i < PROGRAM_MAX) { // finish list
			strcpy(ProgramArray[i],"");
			programTime[i] = -1;
			i++;
			}
		return;
		}

//----------------------------
//	program.lst exists
//----------------------------

	char tmp[2048],tme[32];
	SP->scount=0;

	while (1) {

		tmp[0] = 0;

		if (fgets(tmp,2048,s1) == NULL) break; // read program.lst line
		if (strlen(tmp) < 3) continue;
		if (tmp[0] == '#') continue;

		tmp[strlen(tmp)-1] = 0; // remove newline char

		programData[SP->scount]->play = 1;
		strcpy(cmd, tmp);
		programTime[SP->scount] = -1;

		if (strncmp(cmd, "smb:", 4) == 0) {	// samba netork file

			hexcleaner(cmd);

			if (strlen(cmd) >= PGM_MAX) {
				printf("*** File reference too long: **%s**\n",cmd);
				continue; // does not exist
				}

			strcpy(ProgramArray[SP->scount],cmd);	// record file name

			(SP->scount)++;

			if (SP->scount > PROGRAM_MAX-20) {
				(SP->scount)--;
				printf("*** More than %d lines in program.lst \n", PROGRAM_MAX);
				break;
				}
			continue;
			}

//--------------------------------album load ----------------------------------------------

		if (cmd[strlen(cmd) - 1] == '/' ) { // directory found - final / char

			FILE *f1;
			char tmp[2048],tmp1[2048];

			if (cmd[0] != '/') { // marker
				strcpy(ProgramArray[SP->scount], cmd );// record file name
				programData[SP->scount]->play = 1;
				programTime[SP->scount] = -1;
				(SP->scount)++;
				continue; // files already loaded
				}

			strcpy(tmp, cmd);

			for (i = strlen(tmp) - 2; i > 0; i--) if (tmp[i] == '/') break;

//			strcpy(ProgramArray[SP->scount], &tmp[i+1] ); // record marker
//			programData[SP->scount]->play = 1;
//			(SP->scount)++;

			strcat(tmp, "playlist.m3u"); // onto directory, attach playlist.m3u
			f1 = fopen(tmp, "r");
			if (!f1) {
				printf("*** file %s not found\n", tmp);
				continue;	// playlist not found
				}

			if (SP->scount > PROGRAM_MAX-20) {
				(SP->scount)--;
				continue; // bail out - not enough space
				}

			while (fgets(tmp, 2048, f1)) { // read playlist
				tmp[strlen(tmp)-1] = 0;	// new line
				if (tmp[strlen(tmp)-1] == ' ')
					tmp[strlen(tmp)-1] = 0;	// blank due tocopy/paste
				if (strlen(tmp) == 0 ) continue;

				if (tmp[0] == '#') continue; // comment


				if (strlen(tmp) >= PGM_MAX) {
					printf("*** Playlist file reference too long: **%s**\n", tmp);
					continue;
					}

				if (tmp[0] == '/') strcpy(tmp1, tmp);	// full path name
				else {	// add path info
					strcpy(tmp1, cmd);
					strcat(tmp1, tmp);
					}

				if ( access(tmp1, R_OK) ) {
					printf("*** Playlist file not found/unreadable: **%s**\n", tmp1);
					continue; // does not exist
					}

				strcpy(ProgramArray[SP->scount],tmp1);	// record file name

				programData[SP->scount]->play = 1;

				(SP->scount)++;
				if (SP->scount > PROGRAM_MAX - 20) {
					(SP->scount)--;
					}
				}
			fclose (f1);
			continue;
			}

//--------------------------------album load end ------------------------------------------

		else if ( strncmp(cmd,"https",5) != 0 &&
		          strncmp(cmd,"http",4) != 0 &&
		          strncmp(cmd,"file",4) != 0 &&
		          cmd[strlen(cmd)-1] != '/' ) {

			if (filetype(cmd) == UNRECOGNIZED ) {
				printf("*** File type not recognized/accepted: %s\n",cmd);
				continue;
				}

			if ( access(cmd, R_OK) ) {
				printf("*** File not found/unreadable: **%s**\n",cmd);
				continue; // does not exist
				}
			}

		if (strlen(cmd) >= PGM_MAX) {
			printf("*** File reference too long: **%s**\n",cmd);
			continue; // does not exist
			}

		strcpy(ProgramArray[SP->scount],cmd);	// record file name

		(SP->scount)++;
		if (SP->scount > PROGRAM_MAX-20) {
			(SP->scount)--;
			printf("*** More than %d lines in program.lst \n", PROGRAM_MAX);
			break;
			}
		}

	i = SP->scount;

	while (i < PROGRAM_MAX) { // finish list
		strcpy(ProgramArray[i], "");
		programTime[i] = -1;
		i++;
		}

	fclose (s1);
	printf("*** program.lst loader\n");

//------------------------------------------
//	scount is count of actual entries
//	PROGRAM_MAX is size of array
//------------------------------------------

	printf("SP->scount=%d PROGRAM_MAX=%d\n", SP->scount,PROGRAM_MAX);
	printf("\n");

	SP->begin = 0;

	if (scroll) { // not 1st time?

//------------------------------------------------------
//		set maximum limit of program scroll bars
//------------------------------------------------------

		gtk_range_set_range (GTK_RANGE(scroll), 0, SP->scount);
		gtk_range_set_range (GTK_RANGE(scroll2), 0, SP->scount);
		gtk_range_set_range (GTK_RANGE(search_scroll), 0, SP->scount);

//------------------------------------------------------
//		set increment of program scroll bars
//------------------------------------------------------

		gtk_range_set_increments (GTK_RANGE(scroll), 1., 1.);
		gtk_range_set_increments (GTK_RANGE(scroll2), 1., 1.);

//------------------------------------------------------
//		mark to beginning
//------------------------------------------------------

		gtk_range_set_value (GTK_RANGE(scroll),SP->begin);
		gtk_range_set_value (GTK_RANGE(scroll2),SP->begin);
		gtk_range_set_value (GTK_RANGE(search_scroll),SP->begin);

		}

	}

