#!/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

# @@.sh libbase.a libzzz.a obj1.o libbbb.a ....	>> merge to $1 lib
set -eu
(
`dirname $0`/mglb -p $@
mv outlib.a $1
)
exit 0

(
set -e
bdir=`pwd`
cd "$bdir"
rm -rf ./tmpdir 2>/dev/null || :
mkdir ./tmpdir
v_base=$1
for i;do
	cd "$bdir"
	cp $i ./tmpdir
	cd ./tmpdir
	ar -x $i 2>/dev/null|| :
	rm ${i%.a}.a 2>/dev/null|| :
	olist=`(ar -t $v_base;ar -t $v_base;ls *.o||:)|sort|uniq -u`
	ar -r $v_base $olist
	rm *.o 2>/dev/null|| :
done
# kick main.o
buf=`nm -AgP $v_base|awk '$2=="main"{print $1}'|sed -e 's@.*\[\(.*\)\].*@\1@g'`
[ "$buf" = "" ] || ar -d $v_base $buf

cd "$bdir"
cp ./tmpdir/$v_base ./
rm -rf ./tmpdir
)
