case 'Today': $interval = '-1 DAY';break;
		case 'Week': $interval = '-7 DAY';break;
		case 'Month': $interval = '-1 MONTH';break;
		case 'Year': $interval = '-1 YEAR';break;
		case 'All': $interval = '-100 YEAR';break;
		default:
			break;
	}

	if(useUTC())
		$intervalQry = "starttime between adddate(utc_timestamp(), interval $interval) and utc_timestamp() ";
	else
		$intervalQry = "starttime between adddate(now(), interval $interval) and now() ";
	
	$conditions = array('conditions' => array("basename like ? AND $intervalQry", '%.mp4'));
	$record = Recorded::all( $conditions );
	error_log("COUNT of RECORDED: ".count($record), 0);
	
	$vselect = array('select' => '*, case releasedate when (releasedate is null) then insertdate else releasedate end as starttime');
	$conditions = array('conditions' => array("filename like ? AND host > ? HAVING $intervalQry", '%.m%4%', ''));
	$video = VideoMetadata::all( array_merge($vselect, $conditions) );
	error_log("COUNT of VIDEOMETADATA: ".count($video), 0);
	
	$items = array();
	$shows = array_values(array_merge($record, $video));
	usort($shows, 'shows_date_compare');
	foreach($shows as $item => $show ){
		$items[] = new item($show);
	}	
	
	if(count($items)){
}else{
	//build category from available groups	

	$group = new category(
		array(XmlEmitter::ATR.'title'=>'Group'
			, XmlEmitter::ATR.'description'=>'Select a Group'
			, XmlEmitter::ATR.'sd_img'=>"$WebServer/$MythRokuDir/images/tab-detach.png"
			, XmlEmitter::ATR.'hd_img'=>"$WebServer/$MythRokuDir/images/tab-detach.png"
			, 'categoryLeaf'=>array()
		)
	);

	$menu = array();
	$results = array();
	
	$rec_cat = Recorded::find_by_sql( "select distinct playgroup from recorded where basename like '%.mp4'" );
	foreach ( $rec_cat as $value ) {
       $results[] = ucwords(str_replace('-', ' ', $value->playgroup));
	}	
	$vid_genre = VideoCategory::find_by_sql( 'select distinct vc.category from videocategory vc join videometadata v on v.category = vc.intid' );
	foreach ( $vid_genre as $value ) {
    	$results[] = ucwords(str_replace('-', ' ', $value->category));   
	}	
	asort($results);
	$results = array_unique($results);

	foreach ( $results as $value ) {
		$parms = array('Group'=>rawurlencode($value));
    	$menu[] = new categoryLeaf( 
    		array(XmlEmitter::ATR.'title'=>$value
    		, XmlEmitter::ATR.'feed'=>"$WebServer/$MythRokuDir/mythtv_group_xml.php?".http_build_query($parms))  
<?php

//get the local info from the settings file
require_once 'settings.php';

if (isset($_GET['basename']))
{
    $basefname = $_GET['basename'];
    $conditions = array('conditions' => array('basename = ? ', $basefname));
    $recordings = Recorded::all($conditions);
    if(count($recordings) != 1) {
        error_log( "There are " . count($recordings) . " items with the basename: $basefname", 0 );
    }else{
        $recording = $recordings[0];
        
        error_log( "here we delete $recording->basename from the database.", 0 ); 
        $recording->delete();
        
        $fname = $recording->storagegroups->dirname . strtok($recording->basename, "."); 
        foreach(glob($fname . "*") as $file){
            error_log( "here we delete $file from the filesystem.", 0 ); 
            unlink($file);
        }
    }
}else{
    error_log("the 'basename' was not passed to this routine!", 0);
}

?>

	else
		$intervalQry = "starttime between adddate(now(), interval $interval) and now() ";
		
	$conditions = array('conditions' => array("basename like ? AND $intervalQry", '%.mp4') );
	if($select == 'New'){
		$conditions['conditions'][0].= " HAVING datediff(recorded.starttime, airdate) < 7";
	}
	$rquery = array(
		'select'=>'recorded.*
			, ifnull( 
				nullif(recorded.originalairdate,0)
				, makedate( (select airdate from recordedprogram where recordedprogram.chanid=recorded.chanid and recordedprogram.starttime=recorded.starttime),1 )
	    	) as airdate'			
	);

	$record = Recorded::all( array_merge($rquery, $conditions) );
	error_log("COUNT of RECORDED: ".count($record), 0);
	
	$vselect = array('select' => '*, case releasedate when (releasedate is null) then insertdate else releasedate end as starttime');
	$conditions = array('conditions' => array("filename like ? AND host > ? HAVING $intervalQry", '%.m%4%', ''));
	$video = VideoMetadata::all( array_merge($vselect, $conditions) );
	error_log("COUNT of VIDEOMETADATA: ".count($video), 0);
	
	$items = array();
	$shows = array_values(array_merge($record, $video));
	usort($shows, 'shows_date_compare');
	foreach($shows as $item => $show ){
		$items[] = new item($show);
	}	
	
	if(count($items)){
}else{
	// build category from available genres	

	$genre = new category(
		array(XmlEmitter::ATR.'title'=>'Genre'
			, XmlEmitter::ATR.'description'=>'Select a Genre'
			, XmlEmitter::ATR.'sd_img'=>"$WebServer/$MythRokuDir/images/bookmark-5.png"
			, XmlEmitter::ATR.'hd_img'=>"$WebServer/$MythRokuDir/images/bookmark-5.png"
			, 'categoryLeaf'=>array()
		)
	);

	$menu = array();
	$results = array();
	
	$rec_cat = Recorded::find_by_sql( "select distinct category from recorded where basename like '%.mp4'" );
	foreach ( $rec_cat as $value ) {
       $results[] = ucwords(str_replace('-', ' ', $value->category));
	}	
	$vid_genre = VideoMetadata::find_by_sql( "select (case when g.genre is null then 'Default' else g.genre end) as genre from videometadata v left join videometadatagenre a on a.idvideo = v.intid left join videogenre g on g.intid = a.idgenre");
	foreach ( $vid_genre as $value ) {
    	$results[] = ucwords(str_replace('-', ' ', $value->genre));   
	}	
	asort($results);
	$results = array_unique($results);

	foreach ( $results as $value ) {
		$parms = array('Genre'=>rawurlencode($value)); 
    	$menu[] = new categoryLeaf( 
    		array(XmlEmitter::ATR.'title'=>$value
    		, XmlEmitter::ATR.'feed'=>"$WebServer/$MythRokuDir/mythtv_genre_xml.php?".http_build_query($parms)) 
        }												
	} else {
		throw new Exception("unknown file: $file");
	}
} elseif (isset($_GET['preview'])) { //send a key of chanid and starttime. 
	$preview = rawurldecode($_GET['preview']);
	$chanid = ltrim(substr($preview,0,6),'_');
	$starttime = substr($preview,6);
	
	$rawstarttime = str_replace(' ', 'T', $starttime);
	
	if(defined('_DEBUG')) error_log(">>>PREVIEW: chanid $chanid : startime $rawstarttime", 0);
	
	if($chanid && $starttime) {		
		$conditions = array('conditions' => array('chanid=? and starttime=? ', $chanid, $rawstarttime)); 
		$record = Recorded::first( $conditions );		
		$file = $record->storagegroups->dirname . $record->basename . '.png'; 
		$t = "123";
		$tfile = $record->storagegroups->dirname . $record->basename . ".$t.png"; 
    	if(!file_exists($file)) { //generate preview images since the user may not be invoking this from myth frontend
    		//file_get_contents($MythContentSvc . 'GetPreviewImage' . "?ChanId=$chanid&StartTime=$rawstarttime");
    		file_get_contents($MythContentSvc . 'GetPreviewImage' . "?ChanId=$chanid&StartTime=$rawstarttime&SecsIn=$t");
    		rename( $tfile, $file );
//     		error_log(
//     			"*** " . implode( '|',
// 			    	get_headers(
// 						$MythContentSvc . 'GetPreviewImage' . rawurlencode("?ChanId=$chanid&StartTime=$rawstarttime")
// 					)	
// 				)			
// 			,0);		
    	}