# -*- mode: Makefile -*-
#
###########################################################################
# This Makefile does some dirty tricks to autodetect many things
# about current OS. It does not do anything specific to pasdoc.
#
# It's contents are mostly taken from fpcmake sources,
# see fpc/utils/fpcm/fpcmake.ini in FPC source tree,
# and from Makefiles generated by fpcmake.
#
# Some handy things defined by this Makefile:
#
# - $(PATHSEP) is set to path seperator on this system.
#
# - $(MKDIRPROG) is set the command that behaves like "mkdir"
#   on POSIX-like environments (so you can pass "-p" to it, and so on).
#
#   Note that this will work even if you're on Windows and have only a
#   subset of MinGW tools that are installed with FPC
#   (and you don't have Cygwin or full MinGW Msys environment available).
#   In such case `make' runs your commands within a Windows shell,
#   so you can't simply write "mkdir", because then it will be understood
#   as Windows shell built-in "mkdir" command.
#   This Makefile will set MKDIRPROG to "gmkdir" command (installed with FPC),
#   (actually even to the full path to "gmkdir" command...)
#   so everything will work.
#
# - $(PACKAGEBASEDIR) is set to base temporary directory on this system,
#   that can be used to create temporary file tree for archives.
#
###########################################################################

###########################################################################
# Things below are a simplified version of section [osdetect] of fpcmake.ini
###########################################################################

# We need only / in the path also remove the current dir,
# also remove trailing /'s
override PATH:=$(patsubst %/,%,$(subst \,/,$(PATH)))

# Detect unix
# Darwin is handled specially
ifneq ($(findstring darwin,$(OSTYPE)),)
inUnix=1 #darwin
SEARCHPATH:=$(filter-out .,$(subst :, ,$(PATH)))
else
# Determine if we've a unix searchpath by looking for a ;
# that normally doesn't exists in the unix PATH var.
ifeq ($(findstring ;,$(PATH)),)
inUnix=1
SEARCHPATH:=$(filter-out .,$(subst :, ,$(PATH)))
else
SEARCHPATH:=$(subst ;, ,$(PATH))
endif
endif

# Add path where make is located
SEARCHPATH+=$(patsubst %/,%,$(subst \,/,$(dir $(MAKE))))

# Search for PWD
# Also sets SRCEXEEXT
PWD:=$(strip $(wildcard $(addsuffix /pwd.exe,$(SEARCHPATH))))
ifeq ($(PWD),)
PWD:=$(strip $(wildcard $(addsuffix /pwd,$(SEARCHPATH))))
ifeq ($(PWD),)
$(error You need the GNU utils package to use this Makefile)
else
PWD:=$(firstword $(PWD))
SRCEXEEXT=
endif
else
PWD:=$(firstword $(PWD))
SRCEXEEXT=.exe
endif

# Path Separator, the subst trick is necessary for the \ that can't exists
# at the end of a line
ifdef inUnix
PATHSEP=/
else
PATHSEP:=$(subst /,\,/)
endif

###########################################################################
# Detecting inCygWin 
# (but not the way like fpcmake does it; fpcmake way doesn't work...)
###########################################################################

ifdef inUnix
ifneq ($(strip $(wildcard $(addsuffix /cygpath$(SRCEXEEXT),$(SEARCHPATH)))),)
inCygWin=1
endif
endif

###########################################################################
# Detecting $(MKDIRPROG), this is copied from Makefile generated by fpcmake
###########################################################################

ifndef MKDIRPROG
MKDIRPROG:=$(strip $(wildcard $(addsuffix /gmkdir$(SRCEXEEXT),$(SEARCHPATH))))
ifeq ($(MKDIRPROG),)
MKDIRPROG:=$(strip $(wildcard $(addsuffix /mkdir$(SRCEXEEXT),$(SEARCHPATH))))
ifeq ($(MKDIRPROG),)
MKDIRPROG= __missing_command_MKDIRPROG
else
MKDIRPROG:=$(firstword $(MKDIRPROG))
endif
else
MKDIRPROG:=$(firstword $(MKDIRPROG))
endif
endif

###########################################################################
# Guessing $(PACKAGEBASEDIR)
###########################################################################

# Temporary directory used for preparing archives
ifdef TEMP
PACKAGEBASEDIR := $(TEMP)

# Under Cygwin, make $(PACKAGEBASEDIR) directory in native format
# (not Cygwin's) because we may use it with some non-Cygwin tools.
# (But still I use "--mixed", not "--windows" param of cygpath, because make
# poorly handles "\" (convering \\ to \ is done in so many places that it's
# hard to handle...)
ifdef inCygWin
PACKAGEBASEDIR := $(shell cygpath --mixed $(PACKAGEBASEDIR))
endif

else
# /tmp is a good guess for Unices, and for Win32 if someone uses Cygwin's make
PACKAGEBASEDIR := /tmp
endif