#!/bin/zsh

# This script is NOT used by Region X but included as a bonus for anyone that finds it useful.
# It only sets the OpenFirmware/EFI variables, it does not modify the DVD Player's SSPD setting
# and it does absolutely nothing else. If you need, you can delete the SSPD from within a shell
# script with this command:
#    defaults delete com.apple.DVDPlayer SSPD
# or
#    defaults delete com.apple.dvdplayer SSPD
# for older MacOS X.

#			Included in
# Version	Region X	Comment
# 1.2		1.7			Added this version history. Added check after changing region settings.
# 1.1		1.6			Now included with Region X in Region X.app/Contents/Resources/region.sh
# 1.0		No			Private version

usage()
{
echo "usage: $0 <changes-left> <region>"
echo "       where changes-left is 0 to 5"
echo "       and         region is 1 to 8"
echo ""
echo "       <region> must be omitted if <changes-left> is 5"
echo "       as this will force the region to \"not set\""
exit 2
}

case "$#" in
1)
  if [ "$1" -ne 5 ]
  then
    usage
  fi
  ;;
2)
  if [ "$1" -eq 5 ]
  then
    usage
  fi
  if [ "$1" -lt 0 -o "$1" -gt 5 ]
  then
    usage
  fi
  if [ "$2" -lt 1 -o "$2" -gt 8 ]
  then
    usage
  fi
  ;;
*)
  usage
  ;;
esac

# magical "secret" constants
x05=0
x14=563218714
x13=281609357
x12=-2006678970
x11=1144144163
x10=-1575411567
x24=-1842765528
x23=1226100884
x22=613050442
x21=306525221
x20=-1994221038
x34=295770273
x33=-1999598512
x32=1147684392
x31=573842196
x30=286921098
x44=-1701729879
x43=-850864940
x42=1722051178
x41=861025589
x40=-1716970854
x54=563317018
x53=281658509
x52=-2006654394
x51=1144156451
x50=-1575405423
x64=-1842634456
x63=1226166420
x62=613083210
x61=306541605
x60=-1994212846
x74=295934113
x73=-1999516592
x72=1147725352
x71=573862676
x70=286931338
x84=-1701598807
x83=-850799404
x82=1722083946
x81=861041973
x80=-1716962662

c=$1
r=$2

if [ "$c" -eq 5 ]
then
  r=0
  rm="not set"
else
  rm=$r
fi

eval "x=\$x$r$c"
n=$(expr 5 - $c)

if [ "`id -u`" -ne 0 ]
then
  echo "You must run this script as root."
  echo "Try:"
  echo " sudo" $0 $*
  exit 1
else
  echo "Setting Region $rm, $c changes left for RPC-1 drives ONLY."
  /usr/sbin/nvram Region-Code=$x Region-Changes=$n
  if [ $? -ne 0 ]
    then
	echo "Error occured while changing region settings."
	fi
fi
