# Copyright (C) Takashi Hirata <webmaster at up-cat.net>
# This is a part of felidlabo MinGW pack.
#   http://up-cat.net/

echo "INFO: MinGW felidlabo pack post-install scripts."

. /etc/profile

flpath=`find / -maxdepth 2 -name xx_env.bat`
flpath=`dirname $flpath`

# patch to original pi.sh and use it
pushd $flpath/bin
{ cp -f /postinstall/pi.sh . && \
  patch --no-backup-if-mismatch --binary -N -i $flpath/patch/flpish.diff && \
  pi.sh ; } \
|| { echo "INFO: something wrong in processing pi.sh." ; exit 1 ; }
popd

mingwpath=/mingw
flenvpath=/etc/flenv
sevenzapath=$flpath/bin/7za913/7za
gccfiles=/etc/gccfiles.txt

cat <<__FL_EOF__>$flenvpath
flpath=$flpath
flinstpath=$mingwpath
mingwpath=$mingwpath
srcpath=/src
sevenzapath=$sevenzapath
gccfiles=$gccfiles
__FL_EOF__

#sevenz="$sevenzapath a -t7z -r -mx=9"

########################################
### utilities

# [in] from
# [in] to
mvdir(){
	pushd $1
	find . -type f -name \* | \
		while read filepath ; do \
			echo "mv: $filepath" ; \
			mkdir -p `dirname $2/$filepath` ; \
			mv -f $filepath $2/$filepath ; \
		done
	popd
}

########################################
### gcc adjustment and install

gcctempdir=$flpath/gcc

rm -f $gccfiles.tmp

if [ -d $flpath/gcc ] ; then
	# we cannot use this "find ... | while read ...; do ... done" method
	# because we use "read" in the loop
	#     find $gcctempdir -maxdepth 1 -type d -name gcc-\* | while read gccdir; do ... ; done
	# reverse-sort to bring up gcc4 to the first, to notify about pthread at the beggining
	gccdirlist=`find $gcctempdir -maxdepth 1 -type d -name gcc-\* | sort -r`
else
	gccdirlist=
fi
for gccdir in $gccdirlist ; do
	echo ""
	echo "INFO: checking gcc in $gccdir"
	pushd $gccdir
	if [ -e include/pthread.h ]; then
		echo ""
		echo "INFO: *** DISABLING PTHREAD FEATURE *** within this gcc."
		echo "      this gcc has libpthread, but the pthread on Win32 requires"
		echo "      special care to use, such as dependency onto the pthread*.dll,"
		echo "      or duty to call some extra functions."
		echo "      well, an unexperienced user will be confused if 'configure' or"
		echo "      something alike auto-detects pthread and decides to use it,"
		echo "      and then outputed .exe does not work how they expect."
		echo "      so now we rename the pthread relative files to disable."
		echo "      you can manually add pthread later."
		echo ""
		echo -n "press Enter to continue"
		read ans
		echo ""
		#pushd include
		#patch -p1 -i $flpath/patch/pthreads-w32-2-8-0-fixposix.diff
		#popd
		#cp -f lib/libpthread.a lib/libpthreadGC2-dll.a
		#$flpath/bin/dllpthread.sh disable $gccdir
		if [ -f include/pthread.h ]; then
			pushd lib
			mv -f libpthreadGC2-static.a libpthreadGC2-static.a.esc
			cp -f libpthread.a libpthread.org.a
			mv -f libpthread.a libpthreadGC2.org.a
			mv -f libpthreadGCE2.a libpthreadGCE2.org.a
			popd
			pushd include
			mv -f pthread.h pthread.org.h
			mv -f sched.h sched.org.h
			mv -f semaphore.h semaphore.org.h
			popd
		fi
	fi
	# file-list to be used for arbitration with multiple GCCs
	find . -type f -name \* >> $gccfiles.tmp
	popd
done

# perform arbitration with multiple GCCs
if [ -s $gccfiles.tmp ]; then
	# extract duplicates
	sort $gccfiles.tmp | uniq -d > $gccfiles

	# move all gcc files to /mingw
	# duplicated file will be stored into special dir
	duplist=`cat $gccfiles`
	gccvers=
	for gccdir in $gccdirlist ; do
		echo ""
		echo "INFO: pre-installing gcc in $gccdir"
		pushd $gccdir
		gccver=`basename $gccdir`
		for dupfile in $duplist ; do
			mkdir -p $mingwpath/$gccver/`dirname $dupfile`
			mv -f $dupfile $mingwpath/$gccver/$dupfile
		done
		gccvers="$gccvers $gccver"
		mvdir $gccdir $mingwpath
		# this does not work as i expect.
		# mv seems not to be able to overwrite existing directories.
		#mv -f * $mingwpath
		popd
	done
	echo "gccvers='$gccvers'" >> $flenvpath
else
	echo ""
	echo "WARNING: no duplicated files. no arbitration."
fi
rm -f $gccfiles.tmp
rm -rf $gcctempdir

# gcc switcher script
cp -f $flpath/bin/gccswitch.sh /bin

### gcc adjustment and install ends
########################################

echo
echo "Recent ffmpeg requires pr.exe, so retrieve it from coreutils"
coreutildir=`find / -maxdepth 1 -type d -name coreutils-\*`
if [ -z "$coreutildir" ] || [ ! -d "$coreutildir" ]; then
	echo ""
	echo "WARNING: coreutils' directory is not found, ignored"
else
	cp -f $coreutildir/bin/pr.exe /bin
	rm -fr $coreutildir
fi

echo
echo "INFO: felidlabo pack prefers static to dynamic(dll) for libraries."
echo "INFO: now escaping libraries for dlls."
$flpath/bin/dllexpat.sh static
$flpath/bin/dllpopt.sh static
#$flpath/bin/dllz.sh static

echo
echo "INFO: fix msys.bat to support the path containing white-spaces."
pushd /
patch --no-backup-if-mismatch --binary -N -i $flpath/patch/flmsysbat.diff
popd

echo
echo "INFO: postinstall finished."
