#!/bin/sh
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

target=$1
debug=$2

input() {
    read response
    echo $response
}

echo "Setup your Rockbox build environment."
echo "http://bjorn.haxx.se/rockbox/"
echo ""

if [ "$target" = "update" ]; then
  target=""
  if [ -f Makefile ]; then
    if { grep "^## Auto" Makefile >/dev/null 2>&1 ; } then
      echo "Existing generated Makefile found. Getting defaults from it."
      target=`grep "^TARGET=" Makefile | cut -d= -f2-`
      debug=`grep "^DEBUG=" Makefile | cut -d= -f2-`
    fi
  fi
fi

if [ -z "$target" ]; then

##################################################################
# Figure out target platform
#

  echo "Enter target platform: (defaults to Recorder)"

  echo "1 - Archos Player old LCD"
  echo "2 - Archos Player/Studio new LCD"
  echo "3 - Archos Recorder"

  getit=`input`;

  case $getit in

   1)
    target="-DARCHOS_PLAYER_OLD"
    ;;
   2)
    target="-DARCHOS_PLAYER"
    ;;

   3)
    target="-DARCHOS_RECORDER"
    ;;

   *)
    target="-DARCHOS_RECORDER"
    ;;
  esac
fi


if [ -z "$debug" ]; then
  ##################################################################
  # Figure out debug on/off
  #
  echo ""
  echo "Build DEBUG version? (y/N)"

  option=`input`;

  case $option in
    [Yy])
      debug="DEBUG=1"
      ;;
    *)
      debug="NODEBUG=1"
      ;;

  esac
fi


##################################################################
# Figure out where the firmware code is!
#

firmfile="crt0.S" # a file to check for in the firmware root dir

for dir in firmware . .. ../firmware ../../firmware; do
  if [ -f $dir/$firmfile ]; then
    firmdir=$dir
    break
  fi
done

if [ -z "$firmdir" ]; then
  echo "This script couldn't find your firmware directory. Please enter the"
  echo "full path to the firmware directory here:"

  firmdir=`input`
fi

##################################################################
# Figure out where the apps code is!
#

appsfile="credits.c" # a file to check for in the apps root dir

for dir in apps . .. ../apps ../../apps $firmdir/apps $firmdir/../apps; do
  if [ -f $dir/$appsfile ]; then
    appsdir=$dir
    break
  fi
done

if [ -z "$appsdir" ]; then
  echo "This script couldn't find your apps directory. Please enter the"
  echo "full path to the apps directory here:"

  appsdir=`input`
fi

pwd=`pwd`;

sed > Makefile \
 -e "s,@FIRMDIR@,${firmdir},g" \
 -e "s,@APPSDIR@,${appsdir},g" \
 -e "s,@DEBUG@,${debug},g" \
 -e "s,@TARGET@,${target},g" \
 -e "s,@PWD@,${pwd},g" \
<<EOF 
## Automaticly generated. http://bjorn.haxx.se/rockbox/

FIRMDIR=@FIRMDIR@
APPSDIR=@APPSDIR@
DEBUG=@DEBUG@
TARGET=@TARGET@
THISDIR="@PWD@"

.PHONE: firmware apps

all: apps

firmware:
	make -C \$(FIRMDIR) TARGET=\$(TARGET) \$(DEBUG) OBJDIR=\$(THISDIR)

apps:
	make -C \$(APPSDIR) TARGET=\$(TARGET) \$(DEBUG) OBJDIR=\$(THISDIR)

clean-firmware:
	make -C \$(FIRMDIR) TARGET=\$(TARGET) OBJDIR=\$(THISDIR) clean

clean-apps:
	make -C \$(APPSDIR) TARGET=\$(TARGET) OBJDIR=\$(THISDIR) clean

clean:
	make clean-firmware clean-apps

EOF

echo "Created Makefile"
