# A Plan9 APE TARGET.
# $Id: T_plan9,v 1.43 2021/03/08 08:55:31 tlaronde Exp $
#
# Copyright 2010-2012, 2016-2017, 2019--2020
#     Thierry LARONDE <tlaronde@polynum.com>
# Public Domain. NO GUARANTEES!

# You need to switch to a restricted POSIX environment by:
# % ape/psh.
# And Plan9/APE masquerades for us, so we take default values, not
# APE specials.
#

#----- TARGET environment dependent

TARGET="plan9"
TARGET_RELEASE=${TARGET_RELEASE:-$(uname -r)}
TARGET_ARCH=${TARGET_ARCH:-$(uname -p)}
TARGET_MACHINE=${TARGET_ARCH:-$(uname -m)} # in fact $cputype
TARGET_TYPE="plan9" # posix, not whole user env; and not unix
TARGET_SUBTYPE=ape
TARGET_2D_TYPE=9draw
TARGET_NCPU=${TARGET_NCPU:-1}

TARGETBINDIR="/$objtype/bin"
SYS_LIB_PATH="/$objtype/lib";
HEADER_SEARCH_PATH="/$objtype/include";

# "SYS" is a magic name: search SYS_LIB_PATH (there may be
# others added.
#
HEADER_SEARCH_PATH="SYS:$HEADER_SEARCH_PATH"
LIB_SEARCH_PATH="SYS"

OBJECT_FORMAT=$(echo $TARGET_ARCH\
	| sed -e 's/^\(.\).*$/\1/' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
OBJECT_FORMAT=Obj${OBJECT_FORMAT}$(echo $TARGET_ARCH | sed 's/^.\(.*\)$/\1/')

LIB_TYPES="static"

# The default substitution expression for translating rkcomp
# fixed library naming scheme in the host supported one. 
# static lib.
#
LIB_A_TR=${LIB_A_TR:-'-e s!\(lib[^/][^/]*\)__\([0-9.]*\)!\1.a!'}

# Static shared.
#
LIB_SSH_TR=${LIB_SSH_TR:-'d'}

# And to find the shared lib name (the default does not care about
# versioning. You'd better define something corresponding to your real
# TARGET!).
#
LIB_DSH_TR=${LIB_DSH_TR:-'-e s!\(lib[^/][^/]*\)__[0-9.]*!\1.so!'}

# Standard libes shall be mapped. The headers are pseudo provided by
# the wrapper; the mathlib is in clib and there shall not be set to
# actual values, since there is some artefacts made by ape.
#
CLIB="NULL NULL"
MATHLIB="NULL NULL"
YACCLIB="liby__ NULL"
LEXLIB="libl__ NULL"

# NO POSIX extensions.
#
ICONVLIB=
RK_PTY_TYPE=

# C99.
#
HAVE_VSNPRINTF=YES

# Some libraries may need to be linked in a dynamically linked program
# since they call functions only in a dynamically linked CLIB.
#
DO_NOT_MAKE_STATIC=

# Some libraries must not be used static even if provided (libdl...).
# But this is OS dependent, not user configurable.
#
DO_NOT_USE_STATIC_LIBES=

# Names. User will be ignored.
#
USER0=${USER0:-glenda}
GROUP0=${GROUP0:-sys}

# See namespace(4)
#
TARGETOPTDIR="/"
TARGETLIBDIR="/lib"
TARGETSBINDIR=$TARGETBINDIR
TARGETDOCDIR="/sys/doc"
TARGETTMPDIR="/tmp"
TARGETSHELL="/bin/ape/sh"
TARGETRMDIR="rm"
SYSTEM_ENCODING=RK_ICONV_UTF_8
T_DIR_SEP="/"
T_PATH_SEP=":"

# Map ftn so called with all fields and args i.e $# == 7.
# ck_status is global and shall be set to non 0 (rkconfig debugging)
# and nothing echoed.
#

# The man related functions are independant from the rootdir where the
# hierarchy of manpages is rooted.

# Tmandir() used in map for setting correct man section subdir.
#
# dst=some_dir/{0,*}
#   -> some_dir/man/ if basename == 0,
#   -> some_dir/man/man$basename if basename != 0.
#
Tmandir()
{
	if test $# -ne 7; then
		log "Incorrect Tmandir ftn call with '$*'"
		ck_status=1
		return
	fi
	 
	action="$1"; ftype="$2"; owner="$3"; mod="$4"; src="$5"; dst="$6";
	# args ignored.

  	rootdir=$(dirname $dst)
	section=$(basename $dst)
  	if test $section -eq 0; then
    	dst="$rootdir/man"
  	else
    	dst="$rootdir/man/man$section"
	fi

	echo "$action" "$ftype" "$owner" "$mod" "$src" "$dst"
}

# Qualifying the files. Add the subdir and the suffix to the file.
# Map substitution ftn (see above).
#
# dst=rootdir/section/manpage
#
Tman()
{
	if test $# -ne 7; then
		log "Incorrect Tman ftn call with '$*'"
		ck_status=1
		return
	fi
	 
	action="$1"; ftype="$2"; owner="$3"; mod="$4"; src="$5"; dst="$6";
	# args ignored.

	manpage=$(basename $dst)
	rootdir=$(dirname $dst)
	section=$(basename $rootdir)
	if test "x$section" = x; then
		log "Incorrect $0 ftn call with '$*'"
		ck_status=1
		return
	fi

	rootdir=$(dirname $rootdir)
	if test $section -eq 0; then
		dst="$rootdir/man/$manpage"
	else
		dst="$rootdir/man/man$section/$manpage.$section"
	fi

	echo "$action" "$ftype" "$owner" "$mod" "$src" "$dst"
}


# For now, the default librkcompsh version for explicating libraries map
# instructions will do.
# It is used only at rkconfig() time, not on the TARGET (but is TARGET
# dependant via the regexp).
#
Tlibexpl()
{
	rk_expl_lib "$@"
}
