#!/bin/sh
cat << 'EEE' > /dev/null
/* Copyright (C) 2021 Momi-g
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
EEE

cat << 'EEE' > /dev/null
#SH_doc
title=readlinkp section=1 repnl=\040
@name readlinkp
@_brief return link resolved $0 + '_' path. posix & portable.
@_syno
	readlinkp [-hHVQf] target_path
@tl;dr
		@(code)@
	#!/bin/sh
	full=`readlinkp "$0"`
	echo "${full%?}"
	
	~$ sh mysrc.sh	#>> /home/abc/mysrc.sh
	~$ mysrc (link)	#>> /home/abc/mysrc.sh
		@()
@_opt
	@(list_o)
	-hHV: usage, version
	-f: this option is ignored. this command always resolves links.
	-Q: output corecode
	@()
@_desc
	readlinkp is mimic of `readlink -f`, but this command always add `_`
	to the return path end. --
	skip symbolic link resolve if the path isnt exist.

@exit_status	suc/fail == 0/not0
@notes	this command doesnt work if `file -h` command output is quoted  
@conforming_to	posix-shell
@copyright
	Copyright (C) 2021 Momi-g --
	License GPLv3+ <https://gnu.org/licenses/gpl.html>
@_ver 2021-09-11 v2.0.0
@_see 'readlink(1)'
#SH_docE
EEE
set -ue

f_usage(){
cat << 'EEE'
HowTo (readlinkp, portable readlink command)
opt: -hH(elp), -V(ersion), -Q(corecode), -f(ignored opt)
------
eg)
 ~$ readlinkp mysrc.sh	#>> /home/abc/mysrc.sh_
 ~$ readlinkp ../mysrc.sh 	#>> /home/abc/mysrc.sh_
 ~$ readlinkp mysrc_l (multihop symbolic link)	#>> /home/abc/mysrc.sh_
 ~$ readlinkp /no/exist/path/../text.txt	#>> /no/exist/text/txt_

 ...returns the link resolved fullpath + end '_' except hardlink.
 -f opt is only for syntax compatibility for readlink.
EEE
exit 0
}

f_usage_H(){
 cat << 'E E'|sed -e'1d;$d'
/*--copyfrom readlinkp.1.txt*/
READLINKP(1)                General Commands Manual               READLINKP(1)



NAME
       readlinkp - return link resolved $0 + _ path. posix & portable.

SYNOPSIS
       readlinkp [-hHVQf] target_path

TL;DR
       #!/bin/sh
       full=`readlinkp "$0"`
       echo "${full%?}"

       ~$ sh mysrc.sh #>> /home/abc/mysrc.sh
       ~$ mysrc (link)     #>> /home/abc/mysrc.sh


OPTIONS
       -hHV   usage, version

       -f     this option is ignored. this command always resolves links.

       -Q     output corecode

DESCRIPTION
       readlinkp is mimic of `readlink -f`, but this command always add `_` to
       the return path end.
       skip symbolic link resolve if the path isnt exist.

EXIT_STATUS
       suc/fail == 0/not0

NOTES
       -

CONFORMING_TO
       posix-shell

COPYRIGHT
       Copyright (C) 2021 Momi-g
       License GPLv3+ <https://gnu.org/licenses/gpl.html>

VERSION
       2021-09-11 v2.0.0

SEE_ALSO
       readlink(1)



                                                                  READLINKP(1)
/*--copyend readlinkp.1.txt*/
E E
exit 0
}

f_version_info(){
cat << 'EEE'
readlinkp v2.0.0
Copyright (C) 2021 Momi-g
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
EEE
exit 0
}

code=$(cat<<'HDOC'
 # readlinkp: Copyright (C) 2021 Momi-g: License GPLv3+ <https://gnu.org/licenses/gpl.html>
readlinkp()(
	b="$1"; LC_ALL=C
	while :;do
		[ "${b##/*}" = "" ] || b="$PWD/$b"
		[ -d "$b" ] && { cd "$b";printf '%s'_ "$PWD";return 0; }
		[ ! -e "$b" ]&&
		(	s=""
			while [ "$b" != "" ]; do
			[ "${b%%*/..}" = "" ] && { b="${b%/*}";b="${b%/*}";continue;}
			[ "${b%%*/.}" = "" ] && { b="${b%/*}";continue;}
			s="/${b##*/}$s";b="${b%/*}"
			done
			printf '%s'_ "$s"
		) && return 0
		cd "${b%/*}"
		[ -L "$b" ]||break; s="$b: symbolic link to @"
		b=`file -h "${b%:*}"|cut -b ${#s}-;printf @`
		b="${b%??}"
	done
	printf '%s/%s' "$PWD" "${b##*/}_"
)
 # readlinkp: end
HDOC
)
eval "$code"

f_err(){ echo "readlinkp: err. $*">/dev/stderr;exit 1; }

readlinkp_main()(
	while getopts ":hHVQf" c; do case "$c" in				
	[?:]) f_err "bad opt: $OPTARG, $*" ;;	
	h) f_usage	;;
	H) f_usage_H	;;
	V) f_version_info	;;
	Q) printf '%s\n' "$code"; exit 0	;;
	f) ;;
	esac; done; shift $(($OPTIND-1))
	
	[ $# = 1 ]|| f_err "detect multi args $#: $*"
	readlinkp "$1"
)

readlinkp_main "$@"	#SH_MAIN




cat<<'EEE'>/dev/null
 change log
 --
2021-09-11  Momi-g	<dmy@dmy.dmy>

	* readlinkp(all): new pkg. v2.0.0
	* (main): fix logic, decrease codesize
	* (brp): fix testcode, ftt()

2018-??-??  Momi-g	<dmy@dmy.dmy>

	* readlinkp: publish v1.0.0

EEE

