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

target=$1
debug=$2

extra_defines="-"

input() {
    read response
    echo $response
}

simul () {

if [ -z "$simver" ]; then

  ##################################################################
  # Figure out win32/x11 GUI
  #
  echo ""
  echo "Build (W)in32 or  (X)11 GUI version? (X)"

  option=`input`;

  case $option in
   [Ww])
     simver="win32"
     ;;
   *)
     simver="x11"
     ;;
   esac
fi

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

 simfile="$simver/lcd-$simver.c" # a file to check for in the uisimulator root dir

 for dir in uisimulator . .. ../uisimulator ../../uisimulator; do
   if [ -f "$dir/$simfile" ]; then
     simdir="$dir/$simver"
     break
   fi
 done

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

   simdir=`input`
 fi

sed > Makefile \
 -e "s,@SIMDIR@,${simdir},g" \
 -e "s,@TARGET@,${target},g" \
 -e "s,@DEBUG@,${debug},g" \
 -e "s,@DISPLAY@,${display},g" \
 -e "s,@KEYPAD@,${keypad},g" \
 -e "s,@PWD@,${pwd},g" \
 -e "s,@SIMVER@,${simver},g" \
 -e "s,@EXTRA_DEFINES@,${extra_defines},g" \
<<EOF 
## Automaticly generated. http://rockbox.haxx.se

SIMDIR=@SIMDIR@
DEBUG=@DEBUG@
TARGET=@TARGET@
DISPLAY=@DISPLAY@
KEYPAD=@KEYPAD@
THISDIR="@PWD@"
SIMVER=@SIMVER@
VERSION=\$(shell date +%y%m%d-%H%M)
EXTRA_DEFINES=@EXTRA_DEFINES@

.PHONY: 

all: sim

sim:
	\$(MAKE) -C \$(SIMDIR) DISPLAY=\$(DISPLAY) KEYPAD=\$(KEYPAD) OBJDIR=\$(THISDIR) VERSION=\$(VERSION) EXTRA_DEFINES=\$(EXTRA_DEFINES)

clean:
	\$(MAKE) -C \$(SIMDIR) DISPLAY=\$(DISPLAY) KEYPAD=\$(KEYPAD) OBJDIR=\$(THISDIR) clean

tags:
	@rm -f TAGS
	make -C \$(SIMDIR) DISPLAY=\$(DISPLAY) KEYPAD=\$(KEYPAD) OBJDIR=\$(THISDIR) tags

EOF

  echo "Created Makefile"

  if [ -d "archos" ]; then
    echo "sub directory archos already present"
  else
    mkdir archos
    echo "created an archos subdirectory for simulating the hard disk"
  fi

}

if [ "$target" = "--help" -o \
     "$target" = "-h" ]; then
  echo "Just invoke the script and answer the questions."
  echo "This script will write a Makefile for you"
  exit
fi

# get our current directory
pwd=`pwd`;

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-`
      extra_defines=`grep "^EXTRA_DEFINES=" Makefile | cut -d= -f2-`

      if [ "$debug" = "SIMULATOR=1" ]; then
        simulator="yes"
        display=`grep "^DISPLAY=" Makefile | cut -d= -f2-`
        keypad=`grep "^KEYPAD=" Makefile | cut -d= -f2-`
        simver=`grep "^SIMVER=" Makefile | cut -d= -f2-`
      fi
    fi
  fi
else

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

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"
    display="-DHAVE_LCD_CHARCELLS"
    keypad="-DHAVE_PLAYER_KEYPAD"
    ;;

   2)
    target="-DARCHOS_PLAYER"
    display="-DHAVE_LCD_CHARCELLS"
    keypad="-DHAVE_PLAYER_KEYPAD"
    ;;

   *|3)
    target="-DARCHOS_RECORDER"
    display="-DHAVE_LCD_BITMAP"
    keypad="-DHAVE_RECORDER_KEYPAD"
    ;;

  esac
fi

if [ "-" = "$extra_defines" ]; then
 if [  "-DARCHOS_RECORDER" = "$target" ] ; then
  
  echo "Loadable fonts support? (N)"
  getit=`input`;
  if [ "y" = "$getit" -o "Y" = "$getit" ] ; then
     extra_defines="-DLOADABLE_FONTS"
     echo "*** Remember to copy the 'system.ajf' file to the root of your Archos!"
  else
     echo "Proportional font support? (N)"
     getit=`input`;
     if [ "y" = "$getit" -o "Y" = "$getit"  ] ; then
        extra_defines="-DLCD_PROPFONTS"
     else
        extra_defines=""
     fi
  fi
 else
    extra_defines=""
 fi
fi

if [ -z "$debug" ]; then
  ##################################################################
  # Figure out debug on/off
  #
  echo ""
  echo "Build (N)ormal, (D)ebug or (S)imulated version? (N)"

  option=`input`;

  case $option in
    [Ss])
      debug="SIMULATOR=1"
      simulator="yes"
      ;;
    [Dd])
      debug="DEBUG=1"
      ;;
    *)
      debug="NODEBUG=1"
      ;;

  esac
fi

if [ "yes" = "$simulator" ]; then
  # we deal with the simulator Makefile separately
  simul
  exit
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

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

FIRMDIR=@FIRMDIR@
APPSDIR=@APPSDIR@
DEBUG=@DEBUG@
TARGET=@TARGET@
THISDIR="@PWD@"
VERSION=\$(shell date +%y%m%d-%H%M)

EXTRA_DEFINES=@EXTRA_DEFINES@
.PHONY: firmware apps

all: firmware apps

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

apps:
	\$(MAKE) -C \$(APPSDIR) TARGET=\$(TARGET) \$(DEBUG) OBJDIR=\$(THISDIR) VERSION=\$(VERSION) EXTRA_DEFINES=\$(EXTRA_DEFINES)

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

tags-firmware:
	\$(MAKE) -C \$(FIRMDIR) TARGET=\$(TARGET) OBJDIR=\$(THISDIR) tags

tags-apps:
	\$(MAKE) -C \$(APPSDIR) TARGET=\$(TARGET) OBJDIR=\$(THISDIR) tags

tags:
	@rm -f TAGS
	\$(MAKE) tags-firmware tags-apps
EOF

echo "Created Makefile"
