/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     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 25, 2025

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

int     empty_dir(char *dir_name) {

	char tmp[8192];
	int i;

	sprintf(tmp, "ls %s | wc", dir_name);

	FILE *f1 = popen(tmp, "r");

	if (f1 == NULL) return 0;

	fscanf(f1, "%d", &i);

	fclose(f1);

	if (i == 0) return 0;

	return 1;
	}

//--------------------------------------------
//	Get the playing time for a media file
//--------------------------------------------

void	on_scan_media_clicked(GtkWidget *w) {

	char tmp[8192];

	printf("Scan video directory\n");

	int i = empty_dir(Music_Home_Directory);

//-------------------------------------------------------
//      populate program.lst
//      full addresses of files
//      system("readlink -f videos/* > program.lst");
//-------------------------------------------------------

	if (i == 0)
		sprintf(tmp, "cat /dev/null > \"%s\"", Program_List);
	else
		sprintf(tmp, "readlink -f \"%s/\"* > %s",
		        Music_Home_Directory, Program_List);

	printf("Directory Scan: %s\n", Music_Home_Directory);
	printf("%s\n", tmp);

	int err_stat = system(tmp);

	if (err_stat != 0) {
//                zippy_says("An error occurred\n");
		printf("*** an error occurred scanning the media directory\n");
		return;
		}

//	Program_List has quotes tjhat work in system() commands but not fcns

	strcpy(tmp, &Program_List[1]); // leading quote
	tmp[strlen(tmp) - 1] = 0; // trailing quote

	LoadProgram(tmp);
//        on_date_sort_clicked(NULL);

	DisplayProgramButtons( );
//        on_update_times_clicked(NULL);

	}

