#!/bin/zsh

# We're going to run this in the Bin working directory...
WORK=`pwd`

export COPYFILE_DISABLE=true


if [[ -f universal-darwin.tar.xz ]]; then
	rm  universal-darwin.tar.xz
fi

if [[ -d universal-darwin ]]; then
	rm -R universal-darwin
fi

mkdir universal-darwin

echo CHANGED BINARIES

cd RawCodeRebuild/x86_64-apple-darwin18.7.0
files=`ls`
for f in $files; do
	if  ! [[ -L $f ]] &&  [[ "$(file $f)" = "$f: Mach-O 64-bit executable x86_64" ]]; then
		
		if [ -e ../../RawCodePrevious/x86_64-apple-darwin18.7.0/$f ]; then
			cmp -s ../../RawCodePrevious/x86_64-apple-darwin18.7.0/$f $f
			status=$?
			if ! [[ $status = 0 ]]; then
				echo $f
				if [[ -e ../aarch64-apple-darwin20.6.0/$f ]]; then
					lipo $f   ../aarch64-apple-darwin20.6.0/$f -create -output ../../universal-darwin/$f
				else
					cp $f ../../universal-darwin/$f
				fi
				
				if [[ $f = mf ]] || [[ $f = xdvi-xaw ]] || [[ $f = dvisvgm ]]; then
					codesign -s   "Developer ID Application: Richard Koch"  --force --timestamp --options=runtime --entitlements=${WORK}/gs-X11.entitlements  ../../universal-darwin/$f
				elif [[ $f = luahbtex ]] || [[ $f = luajithbtex ]] || [[ $f = luatex ]] || [[ $f = luajittex ]]; then
					codesign -s   "Developer ID Application: Richard Koch"  --force --timestamp --options=runtime --entitlements=${WORK}/luahbtex.entitlements  ../../universal-darwin/$f
				else
					codesign -s   "Developer ID Application: Richard Koch" --force --timestamp --options=runtime ../../universal-darwin/$f
				fi
			else
				cp ../../RawCodePrevious/universal-darwin/$f ../../universal-darwin/$f
			fi
		else
			echo BRAND NEW
			echo $f
			if [[ -e ../aarch64-apple-darwin20.6.0/$f ]]; then
					lipo $f   ../aarch64-apple-darwin20.6.0/$f -create -output ../../universal-darwin/$f
				else
					cp $f ../../universal-darwin/$f
				fi
			if [[ $f = mf ]] || [[ $f = xdvi-xaw ]] || [[ $f = dvisvgm ]]; then
				codesign -s   "Developer ID Application: Richard Koch"  --force --timestamp --options=runtime --entitlements=${WORK}/gs-X11.entitlements  ../../universal-darwin/$f
			elif [[ $f = luahbtex ]] || [[ $f = luajithbtex ]] || [[ $f = luatex ]] || [[ $f = luajittex ]]; then
				codesign -s   "Developer ID Application: Richard Koch"  --force --timestamp --options=runtime --entitlements=${WORK}/luahbtex.entitlements  ../../universal-darwin/$f
			else
				codesign -s   "Developer ID Application: Richard Koch" --force --timestamp --options=runtime ../../universal-darwin/$f
			fi
		fi
	fi
		
			
done

echo END OF CHANGED BINARIES

echo ASYMPTOTE

cmp -s ../../RawCodePrevious/asy-Intel ../asy-Intel
status=$?
if ! [[ $status = 0 ]]; then
	lipo ../asy-Intel  ../asy-Arm  -create -output ../../universal-darwin/asy
	codesign -s   "Developer ID Application: Richard Koch" --force --timestamp --options=runtime ../../universal-darwin/asy
else
	cp ../../RawCodePrevious/universal-darwin/asy ../../universal-darwin/asy
fi

cd ../../universal-darwin
ln -s ../../texmf-dist/asymptote/GUI/xasy.py xasy
cd ../RawCodeRebuild/x86_64-apple-darwin18.7.0

echo COPY ALL OTHER SCRIPTS AND LINKS

echo LINKS

for f in $files; do
	if [[ -L $f ]]; then
		cp -R $f ../../universal-darwin/$f
	fi
done

echo SCRIPTS

for f in $files; do
	if  ! [[ -L $f ]] && ! [[ "$(file $f)" = "$f: Mach-O 64-bit executable x86_64" ]] ; then
		cp $f ../../universal-darwin/$f
	fi
done





echo END OF CHANGED SCRIPTS

cd ../../

echo FIX xindy.mem

rm universal-darwin/xindy.mem
cp RawCodeRebuild/x86_64-apple-darwin18.7.0/xindy.mem universal-darwin/xindy-x86_64.mem
cp RawCodeRebuild/aarch64-apple-darwin20.6.0/xindy.mem universal-darwin/xindy-arm64.mem

echo COMPRESSING

(cd  universal-darwin; tar cf - . ) > universal-darwin.tar

../xz --compress universal-darwin.tar

cp universal-darwin.tar.xz RawCodeRebuild/universal-darwin.tar.xz




