#!/usr/bin/perl -w

#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 at http://www.gnu.org/licenses/gpl.txt
#for more details.

#show_region.pl by David Wright (dave at dexy dot org), 
#with minor modifications by Angela Brett
#the region is set in byte 35 of the VIDEO_TS.IFO file on the DVD.
#It's an 8-bit mask, with set bits indicating forbidden regions.
#for more information see
#http://cvs.berlios.de/cgi-bin/viewcvs.cgi/ogle/ogle/vmg/IFO_STRUCTURE?rev=1.6
#http://dvd.sourceforge.net/dvdinfo/ifo.html

#pass it the path to a VIDEO_TS.IFO file, e.g. /Volumes/DVDVolume/VIDEO_TS/VIDEO_TS.IFO

use strict;

my @codes = (
'U.S., Canada, U.S. Territories',
'Japan, Europe, South Africa, and Middle East (including Egypt)',
'Southeast Asia and East Asia (including Hong Kong)',
'Australia, New Zealand, Pacific Islands, Central America, Mexico, South America, and the Caribbean',
'Eastern Europe (Former Soviet Union), Indian subcontinent, Africa, North Korea, and Mongolia',
'China',
'Reserved',
'Special international venues (airplanes, cruise ships, etc.)'
);


my $VIDEO_TS_IFO = $ARGV[0];
my ($buf, @valid, $int, $mask);

open(VIDEO_TS, $VIDEO_TS_IFO) or die "Can't open $VIDEO_TS_IFO\n";

seek VIDEO_TS, 35, 0;;
read VIDEO_TS, $buf, 1;
close VIDEO_TS;

$mask = unpack("B*", $buf);
$int = unpack("C*", $buf);

if( $int ) {
    for(1..8) {
        if( ~$int & 1 ) {
            push @valid, $_;
        }
        $int = $int >> 1;
    }
}

print "$VIDEO_TS_IFO has region mask: $mask\n";
if ( @valid ) {
    print "VIEWABLE IN REGIONS: ";
    while( my $reg = pop @valid ) {
        print "\n$reg ($codes[$reg-1])";
    }
}
else {
    print "VIEWABLE EVERYWHERE. WELCOME TO THE FREE WORLD!\n";
}

