# Copyright 2020 RISC OS Open Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Makefile fragment for Basic applications

INCLUDED_BASICAPP = YES

#
# This makefile provides the following phony targets:
#
#    all  install  resources  rom  install_rom
#
#
# This fragment uses the following macros set by the master makefile.
#
#
# COMPONENT                 (the name of the component)
# TARGET              (opt) (the leafname of the application - otherwise ${COMPONENT})
# ROM_MODULE          (opt) (module filename - otherwise rm.${COMPONENT})
# INSTAPP             (opt) (the application target directory - otherwise ${INSTDIR}.!${COMPONENT})
# INSTDIR             (opt) (the target directory - otherwise <Install$Dir>.${TARGET})
# RESFSDIR            (opt) (actual directory to export non-application-dir resources to - otherwise ${RESDIR}.${COMPONENT})
# RESFSAPPDIR         (opt) (where to register application-dir resources to within ResourceFS - otherwise Apps.!${COMPONENT})
# DIRS                (opt) (stamp object for directory creation - otherwise o._dirs)
# SRCS                (opt) (source files to be concatenated, no bas. prefixes - otherwise ${TARGET})
# INSTTYPE            (opt) (use "tool" or "app" to install executable vs application - defaults to "tool")
# INSTAPP_FILES       (opt) (list of files to be installed in application directory - use InstRes specification rules)
# INSTAPP_DEPENDS     (opt) (list of dependencies to be satisfied before doing application install - ${TARGET} assumed if in INSTAPP_FILES)
# INSTAPP_VERSION     (opt) (list of Messages/!Run/Desc files to insert app version from VersionNum - include in INSTAPP_FILES as well)
# RES_FILES           (opt) (list of files to be installed in ${RESFSDIR} when building a ROM - use InstRes specification rules. Where these conflict with the files in INSTAPP_FILES, prefer a subdirectory named 'ROM')
# RES_DEPENDS         (opt) (list of dependencies to be satisfied before doing resources export - ${TARGET} assumed if in RES_FILES)
# RESAPP_FILES        (opt) (list of files to be installed in ${RESFSAPPDIR} when building a ROM - use InstRes specification rules. Where these conflict with the files in INSTAPP_FILES, prefer a subdirectory named 'ROM')
# CUSTOMINSTALLAPP    (opt) (set to "custom" to override the install rule for resource files)
# CUSTOMINSTALLTOOL   (opt) (set to "custom" to override the install rule for target binary)
# CUSTOMROM           (opt) (set to "custom" to override the rom rules)
#
#

INSTALLDIR    = <Install$Dir>

TARGET       ?= ${COMPONENT}
ROM_MODULE   ?= rm.${COMPONENT}
INSTDIR      ?= ${INSTALLDIR}.${TARGET}
INSTAPP      ?= ${INSTDIR}.!${COMPONENT}
RESFSDIR     ?= ${RESDIR}.${COMPONENT}
RESFSAPPDIR  ?= Apps.!${COMPONENT}
MERGEDRDIR   ?= o._ResData_
RES_AREA     ?= Resources
DIRS         ?= o._dirs
SRCS         ?= ${TARGET}

SRCS_         = $(addprefix bas.,${SRCS})

ifeq ("${INCLUDED_STDTOOLS}","")
include StdTools
endif

all: ${TARGET}
        @${ECHO} ${COMPONENT}: application built

${DIRS} ::
        ${MKDIR} crunched
        ${MKDIR} o
        ${MKDIR} rm
        ${TOUCH} $@

clean ::
        @IfThere crunched Then ${ECHO} ${WIPE} crunched ${WFLAGS}
        @IfThere crunched Then ${WIPE} crunched ${WFLAGS}
        @IfThere o        Then ${ECHO} ${WIPE} o ${WFLAGS}
        @IfThere o        Then ${WIPE} o ${WFLAGS}
        @IfThere rm       Then ${ECHO} ${WIPE} rm ${WFLAGS}
        @IfThere rm       Then ${WIPE} rm ${WFLAGS}
ifneq (${TARGET},${SRCS})
        ${RM} bas.${TARGET}
endif
        ${RM} ${TARGET}
        @${ECHO} ${COMPONENT}: cleaned

install: install_${INSTTYPE}

install_: install_tool
        @${NOP}

INSTAPP_DEPENDS += $(filter ${TARGET},${INSTAPP_FILES})
RES_DEPENDS     += $(filter ${TARGET},${RES_FILES})

install_app${CUSTOMINSTALLAPP}: ${INSTAPP_DEPENDS}
        ${MKDIR} ${INSTAPP}
        ${INSTRES} -I Resources.${USERIF}.${LOCALE},Resources.${USERIF}.UK,Resources.${LOCALE},Resources.UK,Resources ${INSTAPP} ${INSTAPP_FILES}
ifneq (,$(filter Messages,${INSTAPP_VERSION}))
        ${CHMOD} +w ${INSTAPP}.Messages
        ${INSERTVERSION} LocalRes:Messages > ${INSTAPP}.Messages
endif
ifneq (,$(filter Desc,${INSTAPP_VERSION}))
        ${CHMOD} +w ${INSTAPP}.Desc
        ${INSERTVERSION} descmode=1 LocalRes:Desc > ${INSTAPP}.Desc
endif
ifneq (,$(filter !Run,${INSTAPP_VERSION}))
        ${CHMOD} +w ${INSTAPP}.!Run
        ${INSERTVERSION} obeymode=1 LocalRes:!Run > ${INSTAPP}.!Run
        ${SETTYPE} ${INSTAPP}.!Run Obey
endif
        @${ECHO} ${COMPONENT}: application installation complete

install_tool${CUSTOMINSTALLTOOL}: ${TARGET}
        ${MKDIR} ${INSTDIR}
        ${CP} ${TARGET} ${INSTDIR}.${TARGET} ${CPFLAGS}
        @${ECHO} ${COMPONENT}: tool installation complete

resources: ${RES_DEPENDS}
        ${MKDIR} ${RESFSDIR}
ifneq (${RES_FILES},)
        ${INSTRES} -I Resources.${LOCALE}.ROM,Resources.UK.ROM,Resources.ROM,Resources.{LOCALE},Resources.UK,Resources ${RESFSDIR} ${RES_FILES}
endif
ifneq (,$(filter Messages,${INSTAPP_VERSION}))
        ${INSERTVERSION} ${RESFSDIR}.Messages > ${RESFSDIR}._Awk_
        ${CP} ${RESFSDIR}._Awk_ ${RESFSDIR}.Messages ${CPFLAGS}
        ${RM} ${RESFSDIR}._Awk_
        ${TOUCH} -r LocalRes:Messages ${RESFSDIR}.Messages
endif
        @${ECHO} ${COMPONENT}: resources copied to Messages module

rom${CUSTOMROM}: ${ROM_MODULE}
        @${ECHO} ${COMPONENT}: rom module built

install_rom${CUSTOMROM}: ${ROM_MODULE}
        ${CP} ${ROM_MODULE} ${INSTDIR}.${COMPONENT} ${CPFLAGS}
        @${ECHO} ${COMPONENT}: rom module installed

${TARGET}: ${SRCS_} ${DIRS}
ifneq (${TARGET},${SRCS})
        ${FAPPEND} bas.${TARGET} ${SRCS_}
endif
ifneq (,${SQUISH})
        ${BASCRUNCH} -1 bas.${TARGET} crunched.${TARGET}
        ${SQUISH} ${SQUISHFLAGS} -from crunched.${TARGET} -to $@
else
        ${BASCRUNCH} -1 bas.${TARGET} $@
endif
        @${AWK} "BEGIN { print \"${TARGET}: ${SRCS_}\" }" > !Depend

${ROM_MODULE}: ${DIRS}
        ${MKDIR} ${MERGEDRDIR}
        ${INSTRES} -I Resources.${LOCALE}.ROM,Resources.UK.ROM,Resources.ROM,Resources.${LOCALE},Resources.UK,Resources ${MERGEDRDIR} ${RESAPP_FILES}
        ${INSTVIARG} ${MERGEDRDIR} ${RESFSAPPDIR} o._ModGen_
        ${GETVERSION} BasicApp$BuildV BasicApp$FullV BasicApp$Date
        ${DO} ${MODGEN} -date "<BasicApp$Date>" $@ !${COMPONENT} !${COMPONENT} <BasicApp$BuildV> -via o._ModGen_

# EOF
