#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/dvdio.h>
#include <unistd.h>

int main( int argc, char const *argv[] ) {
	int ret;
	int region;
	int fd;
	struct dvd_authinfo dvd;

	if( argc < 2 ) {
		fprintf( stderr, "Usage: %s <dvd device>\n", argv[ 0 ]);
		return -1;
	}

	fd = open( argv[ 1 ], O_RDONLY );
	if( fd < 0 ) {
		fprintf( stderr, "Failed to open %s\n", argv[ 1 ]);
		return -1;
	}

	memset( &dvd, 0, sizeof( struct dvd_authinfo ));
	dvd.format = DVD_REPORT_RPC;
	ret = ioctl( fd, DVDIOCREPORTKEY, &dvd );
	close( fd );
	if( ret < 0 ) {
		fprintf( stderr, "Failed to retrieve region info. Try with a disc in the drive.\n" );
		return -1;
	}
	dvd.region ^= 0xff;
	for( region = 0; dvd.region; dvd.region >>= 1 ) {
		region++;
		if( dvd.region == 1 ) break;
	}
	printf( "vendor resets left: %u\n", dvd.vend_rsts );
	printf( "user changes left: %u\n", dvd.user_rsts );
	printf( "drive region: %u\n", region );
	printf( "rpc type: %u\n", dvd.rpc_scheme + 1 );
	
	return 0;
}
