/*
 *  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 Library 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.
 */
 
/*
 * Peer Georgi, Kristian Mueller 
 * 01.01.2005
 * Version 1.0
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define DEFAULT_TERM 1

int switchToTerminal(int iTermNumber){
	int iFileDescriptor = 0;
	
	if (iTermNumber	<= 0){
		cerr << "Terminal number has to be positive!" << endl;
		return (-1);
	}
	iFileDescriptor = open("/dev/console", O_RDWR);

  if (iFileDescriptor <= 0){
		cerr << "Could not open terminal device!" << endl;
		return (-1);
	}	
	
	cerr << ioctl(iFileDescriptor, VT_ACTIVATE, iTermNumber) << endl;
	close (iFileDescriptor);
	return (1);
}

int main(int iArgC, char** sArgs){
	int iVTNumber = 0;

  if (!(iArgC == 2)){
		switchToTerminal(DEFAULT_TERM); 
		return (1);
	}	
	std::string sCmdLine(sArgs[1]);
	std::stringstream ss(sCmdLine);
	ss >> iVTNumber;
	
	switchToTerminal(iVTNumber);
}