<commit_before>#!/bin/bash
#
#   Sample global post-install hook for the WSL distribution switcher.
#   Supports Arch Linux, APT (Debian, Ubuntu..) and RPM-based (Fedora, CentOS..) distributions.
#   Remove ".sample" from file name for install.py to run it automatically after installation.
#
#   Accepts the following environmental variables:
#       REGULARUSER   -- name of your regular user; sent by install.py automatically.
#       ROOTPASSWD    -- root password to set; password is not reset if nothing is set.
#       SUDONOPASSWD  -- if set to "1", regular user will be added to sudoers with NOPASSWD.
#       WITHOUTPACAUR -- if set to "1", AUR helper pacaur will not be installed under Arch.
#
#   Installs the following packages:
#       locale, apt-utils, dialog -- Debian only, to fix apt/dpkg warnings
#       passwd, sudo -- Both, changes root password and adds regular user to sudoers
#       fakeroot, pacaur, base-devel -- Arch Linux only, patched fakeroot to use TCP instead of Unix sockets
#       tar, gzip, bzip2, xz, squashfs-tools, vim, git, tmux, curl, wget -- Both, but not crucial per-se
#

logerr () { echo -e "\e[91m[!]\e[39m $@" 1>&2; }
logwrn () { echo -e "\e[93m[!]\e[39m $@"; }
log ()    { echo -e "\e[92m[*]\e[39m $@"; }

# detect package manager

log "Detecting operating system..."

DEB=$(test -f /usr/bin/dpkg && /usr/bin/dpkg --search /usr/bin/dpkg >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
RPM=$(test -f /usr/bin/rpm && /usr/bin/rpm -q -f /usr/bin/rpm >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
PAC=$(test -f /usr/bin/pacman && /usr/bin/pacman -Qo /usr/bin/pacman >> /dev/null 2>&1; test "$?" != "0"; echo "$?")

if [[ "${DEB}" == 1 ]]; then
	export DEBIAN_FRONTEND=noninteractive

	mgr="apt-get -y"
	mgrinst="${mgr} install"

	echo -e 'Dpkg::Progress-Fancy "1";\nAPT::Color "1";' >> /etc/apt/apt.conf.d/99progressbar
elif [[ "${RPM}" == 1 ]]; then
	if ! type -P dnf >/dev/null; then
		mgr="dnf -y"

		echo -e "\ncolor=always" >> /etc/dnf/dnf.conf
		#echo -e "\nfastestmirror=true" >> /etc/dnf/dnf.conf
	else
		mgr="yum -y"

		echo -e "\ncolor=always" >> /etc/yum.conf
		#echo -e "\nfastestmirror=true" >> /etc/yum.conf
	fi
	mgrinst="${mgr} install"
elif [[ "${PAC}" == 1 ]]; then
	mgr="pacman --noconfirm"
	mgrinst="${mgr} --needed -S"

	sed -i 's/^#Color$/Color/' /etc/pacman.conf

	# fake chroot() for pacman in order to prevent installation failures
	echo <KEY> | base64 -d | xz -d > /lib64/libmockchroot.so
	echo '/lib64/libmockchroot.so' >> /etc/ld.so.preload

	# it's shipped as a binary blob, since otherwise it would require gcc to compile,
	# which is not available before pacman is set up. to recreate blob above, run:
	#   wget https://gist.githubusercontent.com/RoliSoft/84813cc353caec614dee8bf74c1b09ef/raw/409978e320084e645b60d86dff7775dc06e9bc1e/libmockchroot.c -O libmockchroot.c
	#   gcc libmockchroot.c -shared -fPIC -ldl -o libmockchroot.so
	#   strip -s libmockchroot.so
	#   cat libmockchroot.so | xz | base64
else
	echo "Unsupported operating system." 1>&2; exit 1
fi

# update to prevent installation failures later on

log "Initiating system upgrade..."

if [[ "${DEB}" == 1 ]]; then
	${mgr} update && ${mgr} dist-upgrade && ${mgr} install apt-utils dialog locales
elif [[ "${RPM}" == 1 ]]; then
	${mgr} upgrade
elif [[ "${PAC}" == 1 ]]; then
	${mgr} -Sy
	${mgrinst} archlinux-keyring
	${mgr} -Su
	pacman-db-upgrade
	${mgr} -S ca-certificates-mozilla
fi

# fix locale warnings

if [[ "${DEB}" == 1 || "${PAC}" == 1 ]]; then
	log "Fixing locale warnings..."

	if [[ -f /usr/share/i18n/charmaps/UTF-8.gz ]]; then
		if ! type -P gzip >/dev/null; then
			${mgrinst} gzip
		fi

		gzip -d /usr/share/i18n/charmaps/UTF-8.gz
	fi

	if ! grep -q -E "^\s*en_US\.UTF-8" /etc/locale.gen; then
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
	fi

	locale-gen
fi
<commit_msg>Replaced chroot mocker to fakechroot package, which actually hooks file I/O functions and rewrites the paths.<commit_after>#!/bin/bash
#
#   Sample global post-install hook for the WSL distribution switcher.
#   Supports Arch Linux, APT (Debian, Ubuntu..) and RPM-based (Fedora, CentOS..) distributions.
#   Remove ".sample" from file name for install.py to run it automatically after installation.
#
#   Accepts the following environmental variables:
#       REGULARUSER   -- name of your regular user; sent by install.py automatically.
#       ROOTPASSWD    -- root password to set; password is not reset if nothing is set.
#       SUDONOPASSWD  -- if set to "1", regular user will be added to sudoers with NOPASSWD.
#       WITHOUTPACAUR -- if set to "1", AUR helper pacaur will not be installed under Arch.
#
#   Installs the following packages:
#       locale, apt-utils, dialog -- Debian only, to fix apt/dpkg warnings
#       passwd, sudo -- Both, changes root password and adds regular user to sudoers
#       fakeroot, pacaur, base-devel -- Arch Linux only, patched fakeroot to use TCP instead of Unix sockets
#       tar, gzip, bzip2, xz, squashfs-tools, vim, git, tmux, curl, wget -- Both, but not crucial per-se
#

logerr () { echo -e "\e[91m[!]\e[39m $@" 1>&2; }
logwrn () { echo -e "\e[93m[!]\e[39m $@"; }
log ()    { echo -e "\e[92m[*]\e[39m $@"; }

# detect package manager

log "Detecting operating system..."

DEB=$(test -f /usr/bin/dpkg && /usr/bin/dpkg --search /usr/bin/dpkg >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
RPM=$(test -f /usr/bin/rpm && /usr/bin/rpm -q -f /usr/bin/rpm >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
PAC=$(test -f /usr/bin/pacman && /usr/bin/pacman -Qo /usr/bin/pacman >> /dev/null 2>&1; test "$?" != "0"; echo "$?")

if [[ "${DEB}" == 1 ]]; then
	export DEBIAN_FRONTEND=noninteractive

	mgr="apt-get -y"
	mgrinst="${mgr} install"

	echo -e 'Dpkg::Progress-Fancy "1";\nAPT::Color "1";' >> /etc/apt/apt.conf.d/99progressbar
elif [[ "${RPM}" == 1 ]]; then
	if ! type -P dnf >/dev/null; then
		mgr="dnf -y"

		echo -e "\ncolor=always" >> /etc/dnf/dnf.conf
		#echo -e "\nfastestmirror=true" >> /etc/dnf/dnf.conf
	else
		mgr="yum -y"

		echo -e "\ncolor=always" >> /etc/yum.conf
		#echo -e "\nfastestmirror=true" >> /etc/yum.conf
	fi
	mgrinst="${mgr} install"
elif [[ "${PAC}" == 1 ]]; then
	mgr="pacman --noconfirm"
	mgrinst="${mgr} --needed -S"

	sed -i 's/^#Color$/Color/' /etc/pacman.conf

	# fake chroot() for pacman in order to prevent installation failures during initial update
	echo <KEY>rWR40nheG+NPMOYP2O3RqQPtYbHjZIQJxr9UkrzvjcpgcUghdBkJygAAAAAAmaFzKeD+DBsAAfkLgDAAALb+Kk+xxGf7AgAAAAAEWVo=' | base64 -d | xz -d > /lib64/libmockchroot.so
	echo '/lib64/libmockchroot.so' > /etc/ld.so.preload

	# it's shipped as a binary blob, since otherwise it would require gcc to compile,
	# which is not available before pacman is set up. to recreate blob above, run:
	#   wget https://gist.githubusercontent.com/RoliSoft/84813cc353caec614dee8bf74c1b09ef/raw/409978e320084e645b60d86dff7775dc06e9bc1e/libmockchroot.c -O libmockchroot.c
	#   gcc libmockchroot.c -shared -fPIC -ldl -o libmockchroot.so
	#   strip -s libmockchroot.so
	#   cat libmockchroot.so | xz | base64
else
	echo "Unsupported operating system." 1>&2; exit 1
fi

# update to prevent installation failures later on

log "Initiating system upgrade..."

if [[ "${DEB}" == 1 ]]; then
	${mgr} update && ${mgr} dist-upgrade && ${mgr} install apt-utils dialog locales
elif [[ "${RPM}" == 1 ]]; then
	${mgr} upgrade
elif [[ "${PAC}" == 1 ]]; then
	# update db and install super-essentials

	${mgr} -Sy
	${mgrinst} archlinux-keyring fakechroot

	# switch primitive chroot mocker which always returns true,
	# to fakechroot which actually rewrites the paths

	echo '/usr/lib/libfakeroot/fakechroot/libfakechroot.so' > /etc/ld.so.preload

	# continue with system upgrade

	${mgr} -Su
	pacman-db-upgrade
	${mgr} -S ca-certificates-mozilla
fi

# fix locale warnings

if [[ "${DEB}" == 1 || "${PAC}" == 1 ]]; then
	log "Fixing locale warnings..."

	if [[ -f /usr/share/i18n/charmaps/UTF-8.gz ]]; then
		if ! type -P gzip >/dev/null; then
			${mgrinst} gzip
		fi

		gzip -d /usr/share/i18n/charmaps/UTF-8.gz
	fi

	if ! grep -q -E "^\s*en_US\.UTF-8" /etc/locale.gen; then
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
	fi

	locale-gen
fi

{'fraction_non_alphanumeric': 0.16487148102815177, 'fraction_numerical': 0.025703794369645042, 'mean_word_length': 3.3073273589878758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5191770', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3097, 'n_words': 898}