#!/bin/bash
# 13/08/2013
#This is dicamarques edition of the script with mac changed to the ones from the list here https://docs.google.com/spreadsheet/lv?key=0AvvH5W4E2lIwdEFCUkxrM085ZGp0UkZlenp6SkJablE and easier wlan config 
#
# wifi_zone_new
#  A script that emulates a Nintendo Zone in a way that you can actually connect to
#  other people all over the world.
#
# Initial version written by Somebunny (9. August 2013).
#

#
# documentation (sort of)
#
# You will need the following packages/programs:
#  - rfkill
#  - dnsmasq (will be killed if already running)
#  - hostapd (will be killed if already running)
# You should not need any additional configuration work.
#
# This script MUST be run as root, or using sudo, reconfiguring network
#  interfaces does not seem to work when run by non-root.
#
# Please adapt the following sections to your own computer:
#  - variables "zone" and "world", found below
#  - you can add hotsopts in "InitZone()", just copy what is there
#
# Usage: call the script with one extra parameter that describes the
#  MAC address to use. I have prepared some options that work from
#  the thread in the gbatemp forums.
#
# Shutdown: call the script with the parameter "stop".
#
#
# A first attempt to organise everything in a somewhat smarter way.
#

# First, some obligatory checks.
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root or with sudo."
   echo "I don't like this either, but some calls here are really picky!"
   exit 1
fi

# some global settings; you should only need to adapt them for your system once
#  * this is the network interface used for your custom AP; must be wireless
zone=$2
#  * this is the network interface used to access the internet; can be almost anything
world=$3

# some local variables; using default values so that something is there
ssid="attwifi"
mac=4E:53:50:4F:4F:46
country=DE
name="Main MAC (All/Any)"

#
# local function that sets up the local variables;
#  crude but better than having to change the script every time
# NOTE: this part should be heavily modified so that it doesn't
#  depend on a static config. Maybe something with an external
#  file or something, so you don't have to share your relay point
#  with the entire world if you don't want to.
#
InitZone() {
  # kill all existing support tools
  killall dnsmasq 2> /dev/null
  killall hostapd 2> /dev/null
  # flush routing entries
  iptables --flush
  # deconfigure network interface
  /sbin/ifconfig $zone down
  case $1 in
    "stop")
      # emergency exit - restore old network state
      echo "Stopping Nintendo Zone hotspot"
      /etc/init.d/network-manager restart # yeah... didn't find a better way to fix it...
      exit
    ;;
    "random") #thanks to duke_srg for this. Edit the title line with the games you have. The default has Mk7, Super Mario 3D world and AC:NL
#For more codes see https://docs.google.com/spreadsheet/ccc?key=0Ajsweg1Cjr5_dGhlaDNfd2Fid0ZpNTBYQ2pwVUlLT3c#gid=0 just add the ctr code here
#everytime you run random it picks a different mac, so once you get mk, then ac...
TITLES=AMKAREEGD
BASE=NSP
RANDOM=$(head -c 2 /dev/urandom | hexdump -e '1/2 "%u"')
CTR=$(($RANDOM%$(($(expr length $TITLES)/3))))
mac=$(echo $BASE$TITLES | cut -c 1-3,$(($CTR*3+4))-$(($CTR*3+6)) | hexdump -e '6/1 "%02X:"' | head -c 17)
      ssid="attwifi"
      country=DE
      name="Random MAC from custom title list"
    ;;
    "default")
      # default settings, my old WiFi card that recently died