#!/bin/dash
cat << 'EEE' > /dev/null
/* ljoin .... line join, conbine overlapped key lines. bourne-shell script
 * Copyright (C) 2018 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

#optcheck--------
buf=`rdopt ":hk:a:" "$@"`
eval "$buf"
if [ $? -ne 0 ] ; then echo "$0: optErr see -h. sleep 1000" >/dev/stderr;sleep 1000; exit 1; fi
eval "$buf"
#echo "___$buf"
#exit 0	#0.040

pos="$opt_k"
addh="$opt_a"
if [ "$addh" = "" ] ; then
	addh=" "
fi

if [ "$pos" = "" ] ; then
	err=1
fi

if [ "$opt_h" = "1" ] || [ "$err" = "1" ] ; then
cat << 'EEE'
HowTo (line join, combine dup key lines. bourne-shell script)
opt: -h, -k(ey), -a(dd header, default is " ") 
------
ex.)
cat << @@@ | ljoin -a " @ " -k 2
q 12 AAA
w zz BBB
e 12 CCC
y 12 FFF
@@@

>>>
q 12 AAA @ e CCC @ y FFF
w zz BBB

...support only stdin/stdout.
EEE
exit 0
fi

# ---main

# sq=$1
# buf=`echo "$sq" | tr -d "123456789" `
# if [ "$buf" != "" ] ; then
# 	echo "$0: err. see -h. sleep 1000" >/dev/stderr ; sleep 1000
# 	exit 1
# fi
# shift

sq=12
# seq arr. print seqence config... not use
sarr=`echo "$sq" | sed -e 's#.#& #g'`
scount=`echo "$sarr" | tr ' ' '\n' | awk '$1 != "" {print $0}' | wc -l `
if [ "$scount" = "" ] ; then
	scount="0"
fi


# use ARGV. delete ins not shift complex array handling.
# input: smax(5 etc) 2 4 1 1 2	agmax(6 etc) aaa bbb ccc ... fff
#          ARGV[1]   2 3 4 5 6    ARGV[6]     7   8   9  ...
#                 s[1] 2 3 4 5             ag[1]  2   3  ...
#

pos=$(( pos + 1 ))	#for add linenum head
# addcmd="cat -"
# if [ "$nflg" = "" ] ; then
 	addcmd="sed -e 's#^[0123456789]\{1,\}[ ]##g'"
# fi

awk '{print NR " " $0}' | sort -sb -k $pos,$pos -k 1,1n |
awk -v pos=$pos -v addh="$addh" '
BEGIN{
	smax=ARGV[1]		# 0="awk"
	for(i=1;i<=smax;i++){
		s[i]=ARGV[1+i]
	}
	
	agmax=ARGV[1+smax+1]
	for(i=1;i<=agmax;i++){
		pp[i]=ARGV[(1+smax+1)+i]
		p[i+2]=pp[i]	#p1,2 uses for lines.
	}
	
# del ...initialize array
	for(i=1;i<ARGC;i++){
		delete ARGV[i]
	}
	str=0	#g_def
}
# ---

$pos == bfkey && NR != 1 {
#	print "@@"
	buf=$0		#save
	
	# remove key & numbers
	$pos="\n"
	$1="\n"
	gsub(/\n /, "", $0)
	gsub(/ \n/, "", $0)
	gsub(/\n/, "", $0)
	p[2]=addh $0
	$0=buf
	
	str=""
	for(i=1; i<=smax; i++){
		str=sprintf("%s%s", str, p[s[i]])
	}
	p[1]=str
}
	
$pos != bfkey && NR != 1 {
#	print NR"@@p"
	if(NR != 1){print p[1]}
	p[1]=$0
}

NR == 1 { p[1]=$0 }

#save compare key 
{	
	bfkey=$pos	#..$3 etc
}

END{
	print p[1]
}' $scount $sarr $# "$@" | sort -n | eval "$addcmd"

# awk ... arg1 arg2 ... pass as parametor
# extend for add user args

# echo "
# 123 aaa
# 222 nnn
# 222 yyy
# 5dg	mmm
# " | ljoin 12
