# See README for details

# Installs items described in the README.
#

# I don't think this is used here anymore:
#include ../../include/make/SDKdefs

default headers install all:

SYSMENU_FILE = sysmenu.zip

IOS_VERS = 248.0.0

ifdef RVL_64
DEFAULT_IOS = firmware.64.$(IOS_VERS).wad
PLATFORM=RVL_64
else
DEFAULT_IOS = firmware.128.$(IOS_VERS).wad
PLATFORM=RVL_128
endif

#  server is NG build server 'iose-3'
SERVER      = iose-3
SOURCE_DIR  = build/latest/$(PLATFORM)/usr/bin/rvl

DVD_ROOT=$(shell getndenv | grep DvdRoot | cut -f2)
DVD_ROOT_UNIX = $(shell cygpath -a $(DVD_ROOT))
VIEWER_DIR    = $(DVD_ROOT_UNIX)/viewer
IOS ?= $(DEFAULT_IOS)

# since we're using rsync, we never want to skip this rule
.PHONY: $(IOS)
$(IOS) : 
	rsync -t guest@$(SERVER)::$(SOURCE_DIR)/$@ ./

%.wad : 
	rsync -t --progress guest@$(SERVER)::$(SOURCE_DIR)/$@ ./

.PHONY: $(SYSMENU_FILE)
$(SYSMENU_FILE):
	wget --no-verbose -N http://filestore/public/NG/sw/tools/$@
	unzip -qo $@

.PHONY: show_fw_location
show_fw_location:
	@echo Will install $(VIEWER_DIR)/$(IOS)
	@echo $(shell ls -l $(VIEWER_DIR)/$(IOS))

.PHONY: get_fw
get_fw: $(VIEWER_DIR)/$(IOS) $(SYSMENU_FILE)
	@echo Firmware copied to:
	@echo $(shell ls -l $(VIEWER_DIR)/$(IOS))

_echo_sysmenu_prereq_msg = @\
	echo "----"; \
	echo "NOTE: We're using firmware 33 to launch sysmenu.elf; this requires firmware 33 to already "; \
	echo "  be present on the NDEV.  If it isn't, you can install it from the Revolution SDK by "; \
	echo "  running RVL_SDK\X86\bin\fwupdate33_4_16_us"; \
	echo "----";

.PHONY: run_sysmenu
run_sysmenu: sysmenu.zip
	$(call _echo_sysmenu_prereq_msg)
	ndrun sysmenu.elf

.PHONY: install_fw
install_fw: get_fw
	$(call _echo_sysmenu_prereq_msg)
	ndrun sysmenu.elf -a -i $(IOS)

.PHONY: install_fw_local
install_fw_local: $(SYSMENU_FILE)
	@echo Installing $(shell ls -l $(VIEWER_DIR)/$(IOS))
	$(call _echo_sysmenu_prereq_msg)
	ndrun sysmenu.elf -a -i $(IOS)

$(VIEWER_DIR)/$(IOS): $(IOS)
	mkdir -p $(VIEWER_DIR)/
	cp -p -f $(IOS) $(VIEWER_DIR)/

REGION?=us
ifeq ("$(REGION)","us")
BACKTOOL:=BackToolForNDEV_RC5_US.gcm
else
ifeq ("$(REGION)","eu")
BACKTOOL:=BackToolForNDEV_RC5_EU_NTSC.gcm
else
ifeq ("$(REGION)","jp")
BACKTOOL:=BackToolForNDEV_RC5_JP.gcm
endif
endif
endif

.PHONY: install_fw_35 install_fw_37 \
        install_wiimenu wiimenu_changer.gcm \
        get_backtool run_backtool

_region_warning = @\
	echo "Attempting to run with country code '$(REGION)'" ;\
	echo "If your NDEV is not of this region, please set environment "; \
	echo "variable REGION to what you require: 'us', 'eu', or 'jp'";

install_fw_35: SDK_NAME=.
install_fw_35:
	$(call _region_warning)
	@fwupdate35_4_16_$(REGION)

install_fw_37: SDK_NAME=ecdk_current
install_fw_37:
	$(call _region_warning)
	@fwupdate37_4_20_$(REGION)

WIIMENU_GCM=wiimenu_changer_071212.gcm

wiimenu_changer.gcm:
	wget --no-verbose -N http://filestore/public/NG/sw/RVL_SDK/$(WIIMENU_GCM)

install_wiimenu: wiimenu_changer.gcm
	ODEM -r -l $(WIIMENU_GCM) &

get_backtool:
	@rm -f $(BACKTOOL)
	wget --no-verbose -N http://filestore/public/NG/BackTool/$(BACKTOOL)

run_backtool: get_backtool
	$(call _region_warning)
	ODEM -r -l $(BACKTOOL) &


default: $(IOS)

clean:
	rm -f $(IOS) $(SYSMENU_FILE) 
	ndstop
	rm -fr sysmenu

clobber: clean 
	rm -f $(DVD_ROOT_UNIX)/viewer/$(IOS) $(SYSMENU_FILE)

# 1: the *name* of the environment variable specifying the directory
# 2: the display name of the thing that will be installed
_ensureDirectory = @\
	if [ -z $(value $1) ]; then\
	    echo "You must set environment variable $(1) to install $(2); see README";\
	    exit 1;\
	fi;\
	if [ ! -w '$(value $1)/viewer' -o ! -d '$(value $1)/viewer' ]; then\
	    mkdir -p '$(value $1)/viewer';\
	fi;

setup:
	$(call _ensureDirectory,DVD_ROOT,firmware)
