export LC_ALL=C

post_install () {
	for dir in mingw32 mingw64
	do
		test ! -d /$dir ||
		test -f /$dir/etc/gitconfig ||
		cat > /$dir/etc/gitconfig <<\GITCONFIG
@@GITCONFIG@@
GITCONFIG
		test ! -d /$dir ||
		test -f /$dir/etc/gitattributes ||
		cat > /$dir/etc/gitattributes <<\GITATTRIBUTES
@@GITATTRIBUTES@@

GITATTRIBUTES

		grep -q '^\*\.docm' /$dir/etc/gitattributes ||
		sed -i -e 's/^\*\.DOCX\(.*\)/&\n*.docm\1\n*.DOCM\1/' \
			-e 's/^\*\.DOT\(.*\)/&\n*.dotm\1\n*.DOTM\1/' \
			/$dir/etc/gitattributes

		# Drop git-wrapper in place of builtins "to save space"
		git_wrapper=/$dir/share/git/git-wrapper.exe
		if test -f $git_wrapper &&
			! cmp -s $git_wrapper /$dir/bin/git-receive-pack.exe
		then
			for b in $(cat /$dir/share/git/builtins.txt)
			do
				rm /$dir/libexec/git-core/$b &&
				ln $git_wrapper /$dir/libexec/git-core/$b

				if test -x /$dir/bin/$b
				then
					rm /$dir/bin/$b &&
					ln $git_wrapper /$dir/bin/$b
				fi
			done
		fi
	done

	grep -q '^db_home: env windows' /etc/nsswitch.conf ||
	sed -i 's/^\(db_home: \)\(windows \)\?\([^w]\)/\1env windows \3/' \
		/etc/nsswitch.conf

	! grep -q '^db_shell: [^#]*cygwin' /etc/nsswitch.conf ||
	sed -i 's/^\(db_shell: \)\(env \)\?\(windows \)\?/\1env windows # /' \
		/etc/nsswitch.conf

	! grep -q '^db_gecos: [^#]*\(cygwin\|windows\)' /etc/nsswitch.conf ||
	sed -i 's/^\(db_gecos: \)\(env \)\?\([^e]\)/\1env # \3/' \
		/etc/nsswitch.conf

	! grep -q '^group: [^#]*db' /etc/nsswitch.conf ||
	sed -i 's/^\(group: \)\(.* \)\?\(db\)/\1\2# \3/' \
		/etc/nsswitch.conf

	! grep -q '^PS1=' /etc/bash.bashrc ||
	sed -i 's/^PS1=/#&/' /etc/bash.bashrc

	grep -q '^# Fixup git-bash in non login env' /etc/bash.bashrc ||
	printf "\n# Fixup git-bash in non login env\nshopt -q login_shell || . /etc/profile.d/git-prompt.sh\n" >> /etc/bash.bashrc

	grep -q git-for-windows etc/pacman.conf ||
	sed -i -e '/^\[mingw32\]/i[git-for-windows]\nServer = https://dl.bintray.com/$repo/pacman/$arch\nSigLevel = Optional\n' etc/pacman.conf

	test i686 != $(uname -m) ||
	case "$(md5sum.exe < /msys2.ico)" in
	292ad5cd*) cp /usr/share/git/msys2-32.ico /msys2.ico;;
	esac

	test ! -f /etc/post-install/05-home-dir.post ||
	rm /etc/post-install/05-home-dir.post

	grep -qw usertemp /etc/fstab ||
	sed -i '$a\
none /tmp usertemp binary,posix=0,noacl 0 0' /etc/fstab
	grep -qw usertemp.*noacl /etc/fstab ||
	sed -i '$s/^none \/tmp usertemp [^ ]*/&,noacl/' /etc/fstab

	! grep -q '^export TERM=' /etc/profile ||
	sed -i 's/^export TERM=/test -n "$TERM" || &/' /etc/profile

	! grep -q ' $1 == ~\* ' /usr/share/bash-completion/bash_completion ||
	sed -i 's/\( \$1 == \|printf -v \$2 \)\(~\* \|~%q \)/\1\\\2/' \
		/usr/share/bash-completion/bash_completion

	! grep -qi '^TMP=' /etc/profile ||
	sed -i 's/^TE\?MP=/#&/i' /etc/profile
	! grep -q '^unset TMP' /etc/profile ||
	sed -i 's/^unset TMP/#&/' /etc/profile
	grep -q '^test -d "$TMPDIR"' /etc/profile || {
		if lineno="$(grep -n '^#TEMP=' /etc/profile)"
		then
			lineno=${lineno%%:*}
		else
			lineno='$'
		fi
		sed -i "$lineno"'a\
case "$TMP" in *\\\\*) TMP="$(cygpath -m "$TMP")";; esac\
case "$TEMP" in *\\\\*) TEMP="$(cygpath -m "$TEMP")";; esac\
test -d "$TMPDIR" || test ! -d "$TMP" || {\
  TMPDIR="$TMP"\
  export TMPDIR\
}\
' /etc/profile
	}

	grep -q 'case "\${MSYS2_PATH_TYPE:-inherit}"' /etc/profile ||
	sed -i 's/{MSYS2_PATH_TYPE:-[^}]*}/{MSYS2_PATH_TYPE:-inherit}/g' \
		/etc/profile

	! grep -qw PERL_PATH /etc/profile.d/perlbin.sh 2>/dev/null ||
	sed -i 's/PERL_PATH/PERL_PATH_EXTRA/g' /etc/profile.d/perlbin.sh
}

post_upgrade () {
	post_install
}
