//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Created 2006-12-03 18:25:17 by Felix Rauscher
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


//this
#include "MediumItem.H"

// c - c++
#include "sstream"
#include <unistd.h>

//vdr
#include "vdr/videodir.h"

 MediumItem :: MediumItem(Medium *med) : cOsdItem(osUnknown), _med(med)
	{
	cout<<_med->GetMountPoint()<<endl;
	SetText(_med->GetMountPoint().c_str());
	SetSelectable(true);
	int lastslash(med->GetMountPoint().find_last_of('/'));
	if(lastslash<0) _subdir=med->GetMountPoint();
	else _subdir=string(med->GetMountPoint(), lastslash+1);
	cout<<_subdir<<endl;
	}


eOSState MediumItem :: ProcessKey(eKeys Key)
	{
	ostringstream cmd;
	ostringstream target;
	cmd<<"touch "<<VideoDirectory<<"/"<<".update";
	target<<VideoDirectory<<"/"<<_subdir;
	if(Key != kOk)
		{
		cout<<"key ignored"<<endl;
		return osUnknown;
		}
	if(_med->IsMounted())
		{
		if(!_med->UnMount())
			{
			Skins.QueueMessage(mtError, "UNMOUNT FAILED!");
			return osContinue;
			}
	//check if target is a symbolic link
		struct stat st;
		stat(target.str().c_str(), &st);
//		if((st.st_mode & S_IFLNK)==0) return osEnd;
	//remove symbolic link
		unlink(target.str().c_str());
		system(cmd.str().c_str());			
		return osEnd;
		}
	if(!_med->Mount())
		{
		Skins.QueueMessage(mtError, "MOUNT FAILED!");
		return osContinue;
		}
//create symbolic link
	symlink(_med->GetMountPoint().c_str(), target.str().c_str());
	system(cmd.str().c_str());
	return osEnd;
	}
