#
# blink1-mini-tool -- minimal blink(1) controller 
#                     for embedded Linuxes or other minimal systems
#
# Arduino Yun
#   - Download Linino SDK from: http://download.linino.org/linino-utils/
#   - Unzip tarball
#   - Set WRT_SDK_HOME environment variable to resulting directory
#   - Do "cd ${WRT_SDK_HOME} && make" to setup SDK
#   - Do "make OS=yun" to build
#   
# OpenWrt / DD-WRT 
#   - Download & unzip the OpenWrt SDK for Linux
#   - Set WRT_SDK_HOME environment variable to resulting directory
#   - Do "cd ${WRT_SDK_HOME} && make" to setup SDK
#   - Do "make OS=wrt" to build
#   - note: "apt-get install lib32stdc++6 lib32z1" if using 32bit SDK on 64bit system
#   - note: if your OpenWrt device is ar71xx-based, try 'make OS=yun' build instead
#
# BeagleBone / BeagleBoard (on Angstrom Linux)
#   - Install USB dev support 
#      "opkg install libusb-0.1-4-dev"
#   - May need to symlink libusb 
#      "cd /lib; ln -s libusb-0.1.so.4 libusb.so"
#
# Mac OS X
#   - Install Xcode and command-line dev tools
#   - Install MacPorts
#   - sudo port install libusb-legacy
#
# Linux (Ubuntu) 
#   - apt-get install gcc-avr avr-libc avrdude java librxtx-java
#
#
#
#

TARGET=blink1-mini-tool

#CC=gcc

# try to do some autodetecting
UNAME := $(shell uname -s)

# assume linux 
ifeq "$(OS)" ""
  OS = linux
endif

ifeq "$(UNAME)" "Darwin"
	OS = macosx
endif

#GIT_TAG="$(strip $(shell git tag | tail -1))"


#################  Mac OSx  ###################################################
ifeq "$(OS)" "macosx"
OPT_HOME := /opt/local/bin
USBFLAGS =  `$(OPT_HOME)/libusb-config --cflags`
USBLIBS  =  `$(OPT_HOME)/libusb-config --libs`
#USBFLAGS =  `$(OPT_HOME)/libusb-legacy-config --cflags`
# get just the path to the static lib
#USBLIBS  =  `$(OPT_HOME)/libusb-legacy-config --libs | cut -d' ' -f1 | cut -c3- `/libusb-legacy.a
# get everything else in --libs
#USBLIBS +=  `$(OPT_HOME)/libusb-legacy-config --libs | cut -d' ' -f 3- `

endif

#################  Linux  ###################################################
ifeq "$(OS)" "linux"
USBFLAGS =   `libusb-config --cflags`
USBLIBS  =   `libusb-config --libs`
endif

#################  Yun OpenWrt  #########################################
ifeq "$(OS)" "yun"

WRT_SDK_HOME := $(HOME)/openwrt/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2
WRT_TOOLCHAIN_ROOT=$(strip $(shell ls -d $(WRT_SDK_HOME)/staging_dir/toolchain-* | tail -1))
WRT_TARGET_ROOT=$(strip $(shell ls -d $(WRT_SDK_HOME)/staging_dir/target-* | tail -1))
STAGING_DIR=$(WRT_SDK_HOME)/staging_dir

CC = $(WRT_TOOLCHAIN_ROOT)/bin/mips-openwrt-linux-gcc
LD = $(WRT_TOOLCHAIN_ROOT)/bin/mips-openwrt-linux-ld
USBFLAGS= -I$(WRT_TARGET_ROOT)/usr/include
USBLIBS = -L$(WRT_TARGET_ROOT)/usr/lib -lusb -lusb-1.0
export STAGING_DIR=$$(STAGING_DIR)

endif

#################  OpenWrt / DD-WRT #########################################
ifeq "$(OS)" "wrt"

WRT_SDK_HOME := $(HOME)/openwrt/OpenWrt-SDK-brcm47xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2
WRT_TOOLCHAIN_ROOT=$(strip $(shell ls -d $(WRT_SDK_HOME)/staging_dir/toolchain-* | tail -1))
WRT_TARGET_ROOT=$(strip $(shell ls -d $(WRT_SDK_HOME)/staging_dir/target-* | tail -1))
STAGING_DIR=$(WRT_SDK_HOME)/staging_dir
# STAGING_DIR used by mipsel-openwrt-linux-gcc

CC = $(WRT_TOOLCHAIN_ROOT)/bin/mip*-openwrt-linux-gcc
LD = $(WRT_TOOLCHAIN_ROOT)/bin/mip*-openwrt-linux-ld
USBFLAGS= -I$(WRT_TARGET_ROOT)/usr/include
USBLIBS = -L$(WRT_TARGET_ROOT)/usr/lib -lusb -lusb-1.0
export STAGING_DIR=$$(STAGING_DIR)

#WRT_SDK_HOME := $(HOME)/OpenWrt-SDK-Linux-i686-1
#CC = $(WRT_SDK_HOME)/staging_dir_mipsel/bin/mipsel-linux-gcc
#LD = $(WRT_SDK_HOME)/staging_dir_mipsel/bin/mipsel-linux-ld
#USBFLAGS = "-I$(WRT_SDK_HOME)/staging_dir_mipsel/usr/include"
#USBLIBS  = "$(WRT_SDK_HOME)/staging_dir_mipsel/usr/lib/libusb.a"

LDFLAGS += -static

endif


##################### Common ###############################################

CFLAGS=	$(OS_CFLAGS) -O -Wall -std=gnu99 $(USBFLAGS)
LIBS=	$(OS_LIBS) $(USBLIBS) $(LDFLAGS)

OBJ=		$(TARGET).o hiddata.o 

PROGRAM=	$(TARGET)$(EXE_SUFFIX)

all: msg $(PROGRAM)

msg: 
	@echo "building for OS=$(OS)"

# symbolic targets:
help:
	@echo "This Makefile works on multiple archs. Use one of the following:"
	@echo "make OS=linux   ... build for Linux"
	@echo "make OS=macosx  ... build for Mac OS X "
	@echo "make clean ..... to delete objects and hex file"
	@echo

test:
	@echo "WRT_TOOLCHAIN_ROOT=$(WRT_TOOLCHAIN_ROOT)"
	@echo "WRT_TARGET_ROOT=$(WRT_TARGET_ROOT)"

$(PROGRAM): $(OBJ)
	$(CC) -o $(PROGRAM) $(OBJ) $(LIBS) 


strip: $(PROGRAM)
	strip $(PROGRAM)

clean:
	rm -f $(OBJ) $(PROGRAM)

.c.o:
	$(CC) $(ARCH_COMPILE) $(CFLAGS) -c $*.c -o $*.o

# shows shared lib usage on Mac OS X
otool:
	otool -L $(TARGET)

foo:
	@echo "OS=$(OS), USBFLAGS=$(USBFLAGS)"
