#!/bin/sh
#set -x


log() {
	logger -t apt-setup "$@"
}
warning() {
	log "warning: $@"
}

TARGET="${1}"


if [ -e ${TARGET}/usr/bin/X ]; then
    if [ `lspci | grep VGA | grep -i " ati " | wc -l` -gt 0 ] ;then
	echo ATI
	if [ -e ${TARGET}/usr/bin/aticonfig ]; then
	    chroot ${TARGET} aticonfig --initial -f
	fi
    fi
fi
if [ -e ${TARGET}/usr/bin/X ]; then
    if [ `lspci | grep VGA | grep -i "nvidia" | wc -l` -gt 0 ] ;then
	echo NVIDIA
	if [ -e ${TARGET}/usr/bin/nvidia-xconfig ]; then
	    chroot ${TARGET} nvidia-xconfig
	fi
    fi
fi


if [ -f ${TARGET}/usr/bin/ntfs-3g ]; then
    ln -sf /usr/bin/ntfs-3g  ${TARGET}/sbin/mount.ntfs
fi

if [ -f ${TARGET}/bin/bash ]; then
    ln -sf /bin/bash ${TARGET}/bin/sh
fi

# setting grub2 params
# Also edit /etc/default/grub
if [ -f ${TARGET}/etc/default/grub ]; then
	mv -f ./newgrub ${TARGET}/etc/default/grub
	sed 's/\#GRUB\_GFXMODE\=640x480/GRUB\_GFXMODE\=1024x768/g' ${TARGET}/etc/default/grub > ./newgrub
	mv -f ./newgrub ${TARGET}/etc/default/grub
	sed -i 's/GRUB_CMDLINE_LINUX_HARDENED=.*/GRUB_CMDLINE_LINUX_HARDENED=\"slub_debug=P page_poison=1 slab_nomerge pti=on user.max_user_namespaces=0 kernel.kptr_restrict=1 vsyscall=none\"/g' ${TARGET}/etc/default/grub
	echo FRAMEBUFFER=y >> ${TARGET}/etc/initramfs-tools/conf.d/splash

# Disable submenu
	echo "GRUB_DISABLE_SUBMENU=y" >> ${TARGET}/etc/default/grub
# Set correct kernel to boot
	cat ${TARGET}/etc/default/grub | sed -e 's/GRUB_DEFAULT=/#GRUB_DEFAULT=/g' > ${TARGET}/etc/default/grub_
	mv -f ${TARGET}/etc/default/grub_ ${TARGET}/etc/default/grub
	kern_ver=`uname -r`
	cat ${TARGET}/boot/grub/grub.cfg | grep "menuentry '" | grep ${kern_ver} | grep -v "recovery mode" | cut -d"'" -f4 > /string_id
	for i in `cat /string_id`; do
		echo "GRUB_DEFAULT=${i}" >> ${TARGET}/etc/default/grub
		break
	done
	rm -f /string_id
fi

# grub2 EFI fix
mount_point_efi=`find ${TARGET} -type f -name grubx64.efi | grep "EFI" | sed -e 's/EFI.*//g'`
if [ $mount_point_efi ]; then
	astra_efi=`ls $mount_point_efi/EFI | grep -i astra`
	if [ ! $astra_efi ]; then
		debian_efi=`ls $mount_point_efi/EFI | grep -i debian`
		if [ $debian_efi ]; then
			mkdir -p $mount_point_efi/EFI/astralinux
			cp $mount_point_efi/EFI/${debian_efi}/* $mount_point_efi/EFI/astralinux/
			rm -rf $mount_point_efi/EFI/${debian_efi}
		fi
	fi
	astra_efi=`ls $mount_point_efi/EFI | grep -i astra`
	if [ ! -d $mount_point_efi/EFI/Boot ]; then
		mkdir -p $mount_point_efi/EFI/Boot
		cp $mount_point_efi/EFI/${astra_efi}/grubx64.efi $mount_point_efi/EFI/Boot/bootx64.efi
		# grub2 EFI fix for grub-efi-ia32
		if [ -f $mount_point_efi/EFI/${astra_efi}/grubia32.efi ]; then
			cp $mount_point_efi/EFI/${astra_efi}/grubia32.efi $mount_point_efi/EFI/Boot/bootia32.efi
		fi
	else
		# grub2 EFI fix for grub-efi-ia32
		if [ -f $mount_point_efi/EFI/Boot/bootia32.efi ]; then
			if [ -f $mount_point_efi/EFI/${astra_efi}/grubia32.efi ]; then
				mv -f $mount_point_efi/EFI/Boot/bootia32.efi $mount_point_efi/EFI/Boot/bootia32.efi_old
				cp $mount_point_efi/EFI/${astra_efi}/grubia32.efi $mount_point_efi/EFI/Boot/bootia32.efi
			fi
		else
			if [ -f $mount_point_efi/EFI/${astra_efi}/grubia32.efi ]; then
				cp $mount_point_efi/EFI/${astra_efi}/grubia32.efi $mount_point_efi/EFI/Boot/bootia32.efi
			fi
		fi
		if [ -f $mount_point_efi/EFI/Boot/bootx64.efi ]; then
			mv -f $mount_point_efi/EFI/Boot/bootx64.efi $mount_point_efi/EFI/Boot/bootx64.efi_old
			cp $mount_point_efi/EFI/${astra_efi}/grubx64.efi $mount_point_efi/EFI/Boot/bootx64.efi
		else
			cp $mount_point_efi/EFI/${astra_efi}/grubx64.efi $mount_point_efi/EFI/Boot/bootx64.efi
		fi
	fi
fi

# Little dirty hack to setupcon
if [ -f ${TARGET}/bin/setupcon ]; then
	cat ${TARGET}/bin/setupcon | sed -e 's/there\.\ >\&2/there\.\ > \/dev\/null/g' > ${TARGET}/bin/setupcon_
	chmod +x ${TARGET}/bin/setupcon_
	mv -f ${TARGET}/bin/setupcon_ ${TARGET}/bin/setupcon
fi

if [ -f ${TARGET}/null ]; then
    rm -f ${TARGET}/null
fi
if [ -f ${TARGET}/.cache ]; then
    rm -fr ${TARGET}/.cache
fi
if [ -f ${TARGET}/.rcc ]; then
    rm -fr ${TARGET}/.rcc
fi

# создание группы astra-admin
GID=$(getent group astra-admin | cut -d: -f3)
chroot ${TARGET} /usr/sbin/groupadd -g ${GID} astra-admin
chroot ${TARGET} /usr/sbin/addgroup --gid 333 astra-console

if [ -f ${TARGET}/usr/share/applications/nautilus.desktop ]; then
    sed -i 's|OnlyShowIn.*||g'  ${TARGET}/usr/share/applications/nautilus.desktop
fi

if [ -f ${TARGET}/etc/polkit-1/localauthority.conf.d/51-debian-sudo.conf ]; then
    sed -i 's|sudo|astra-admin|g'  ${TARGET}/etc/polkit-1/localauthority.conf.d/51-debian-sudo.conf
fi

if [ -f /live ]; then
    mount -o bind /run ${TARGET}/run
fi

if [ -f ${TARGET}/usr/bin/genfly-dmconf.post ] && [ -f ${TARGET}/etc/X11/fly-dm/fly-dmrc ]; then
    chroot ${TARGET} /usr/bin/genfly-dmconf.post
fi

chroot ${TARGET} /usr/bin/dpkg-statoverride --force-all --update --add root crontab 755 /usr/bin/crontab

sed -i 's/#HandlePowerKey=poweroff/HandlePowerKey=ignore/g'  ${TARGET}/etc/systemd/logind.conf
