/*
 * eject.c
 * Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "i2c-dev.h"
#include "xbox.h"
#include <getopt.h>

int fd;
char *fname;
char *devfsname = "/dev/i2c/0";
char *nondevfsname = "/dev/i2c-0";

void checkPIC() {
	if ((fd = open(devfsname, O_RDWR)) < 0) {
		if ((fd=open(nondevfsname,O_RDWR))<0) {
			fprintf(stderr, "Error - failed to open EITHER /dev/i2c/0 or /dev/i2c-0\n    - you must be root and have the correct i2c modules loaded.\n");
			exit(1);
		} else {
			fname = malloc (strlen(nondevfsname));
			strncpy (fname, nondevfsname, strlen(nondevfsname));
		}
	} else {
		fname = malloc (strlen(devfsname));
		strncpy (fname, devfsname, strlen(devfsname));
	}
}

void help() {
	printf("Xbox eject (c) ShALLaX\n\n");
	printf("Usage:\n");
	printf("                   Without any switches the tray will open\n");
	printf("    -t, --close    Closes the CD-ROM tray\n");
	printf("    -h, --help     Print this help message\n");
}

int main(int argc, char *argv[]) {
	int res, traycmd, traystatus, c;
	traycmd = DVD_TRAY_OPEN;
	while (1)
	{
		int option_index = 0;
		static struct option long_options[] = {
			{"close", 0, 0, 0},
			{"help", 0, 0, 0},
			{0, 0, 0, 0}
		};
		c = getopt_long(argc, argv, "::t::h", long_options, &option_index);

		if (c == -1) {
			break;
		}

		switch (c) {
			case 0:
				if (long_options[option_index].name == "close") {
					traycmd = DVD_TRAY_CLOSE;
				}
				if (long_options[option_index].name == "help") {
					help();
					exit(0);
				}
				break;
			case 't':
				traycmd = DVD_TRAY_CLOSE;
				break;
			case 'h':
				help();
				exit(0);
				break;
			case '?':
				printf("'%s' is an unrecognised switch, try see %s --help for details.\n", argv[1], argv[0]);
				exit(1);
			default:
				break;
		}
	}

	checkPIC();

	if ((fd = open(fname, O_RDWR)) < 0) {
		fprintf(stderr, "Could not open i2c device /dev/i2c/0, you must be root!\n");
		exit(1);
	}
	if (ioctl(fd,I2C_SLAVE_FORCE,PIC_ADDRESS) < 0) {
		fprintf(stderr,"Error: Could not set address to %d: %s\n",PIC_ADDRESS,strerror(errno));
		exit(2);
	}

	traystatus = i2c_smbus_read_byte_data(fd, DVD_TRAY_STATE);
	close(fd);

	/* The following commands are for debugging tray status and command */
	/*printf("Status %i\n", traystatus); */
	/*printf("Command %i\n", traycmd); */

	if (traycmd == DVD_TRAY_OPEN) {
		if (traystatus == 0x10) {
			fprintf(stderr, "The drive is already open\n");
			exit(11);
		}
	}
	if (traycmd == DVD_TRAY_CLOSE) {
		if (traystatus != 0x10) {
			fprintf(stderr, "The drive is not open\n");
			exit(10);
		}
	}


	fd = open(fname, O_RDWR);
	ioctl(fd,I2C_SLAVE_FORCE,PIC_ADDRESS);
	res = i2c_smbus_write_byte_data(fd, DVD_EJECT_REG, traycmd);
	close(fd);
	exit(0);
}
