#!/bin/bash

[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"

if mountpoint -q /live/aufs; then
		if [ $(cat /proc/cmdline | grep -ic "init=/") -eq 0 ]; then
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					sync && touch /run/initctl && ( /usr/lib/runit/runit-init 6  | /usr/sbin/init 6 ) && ( /usr/lib/runit/shutdown -r now | /usr/sbin/shutdown -r now ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/s6-rc ]; then
					sync  && ( openvt -c12 -f -- /usr/lib/s6-rc/reboot $@ | openvt -c12 -f -- /usr/bin/s6-linux-init-hpr -r $@ ) && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/66 ]; then
					sync  && ( /usr/lib/s6-66/reboot $@ | /usr/bin/66 reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "dinit") -eq 1 ] || [ $(/sbin/init --version | grep -ic "Dinit") -eq 1 ]; then
					sync  && ( /usr/lib/dinit/reboot $@ | /usr/sbin/dinit-reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ] && [ $(/sbin/init --version | grep -ic "SysV") -eq 1 ]; then
					sync  && ( /usr/lib/sysvinit/init 6 | /usr/sbin/init 6 ) && sleep 3 && ( /usr/lib/sysvinit/reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox reboot -f;
					exit 1
				fi
		else
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					sync && touch /run/initctl && /usr/lib/runit/runit-init 6 && /usr/lib/runit/shutdown -r now && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/s6-rc ]; then
					sync  && ( openvt -c12 -f -- /usr/lib/s6-rc/reboot $@ | openvt -c12 -f -- /usr/bin/s6-linux-init-hpr -r $@ ) && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/66 ]; then
					sync  && ( /usr/lib/s6-66/reboot $@ | /usr/bin/66 reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(cat /proc/cmdline | grep -ic "dinit") -eq 1 ]; then
					sync  && ( /usr/lib/dinit/reboot $@ | /usr/sbin/dinit-reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(cat /proc/cmdline | grep -ic "sysvinit") -eq 1 ]; then
					sync  && /usr/lib/sysvinit/init 6 && sleep 3 && /usr/lib/sysvinit/reboot $@ && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox reboot -f;
					exit 1
				fi	
		fi
else
		if [ $(cat /proc/cmdline | grep -ic "init=/") -eq 0 ]; then
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					sync && touch /run/initctl && ( /usr/lib/runit/runit-init 6  | /usr/sbin/init 6 ) && ( /usr/lib/runit/shutdown -r now | /usr/sbin/shutdown -r now ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/s6-rc ]; then
					sync  && ( /usr/lib/s6-rc/reboot $@ | /usr/bin/s6-linux-init-hpr -r $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/66 ]; then
					sync  && ( /usr/lib/s6-66/reboot $@ | /usr/bin/66 reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "dinit") -eq 1 ] || [ $(/sbin/init --version | grep -ic "Dinit") -eq 1 ]; then
					sync  && ( /usr/lib/dinit/reboot $@ | /usr/sbin/dinit-reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ] && [ $(/sbin/init --version | grep -ic "SysV") -eq 1 ]; then
					sync  && ( /usr/lib/sysvinit/init 6 | /usr/sbin/init 6 ) && sleep 3 && ( /usr/lib/sysvinit/reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox reboot -f;
					exit 1
				fi
		else
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					sync && touch /run/initctl && /usr/lib/runit/runit-init 6 && /usr/lib/runit/shutdown -r now && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/s6-rc ]; then
					sync  && ( /usr/lib/s6-rc/reboot $@ | /usr/bin/s6-linux-init-hpr -r $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "s6-svscan") -eq 1 ] && [ -e /run/66 ]; then
					sync  && ( /usr/lib/s6-66/reboot $@ | /usr/bin/66 reboot $@ ) && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "dinit") -eq 1 ] || [ $(cat /proc/cmdline | grep -ic "dinit") -eq 1 ]; then
					sync  && /usr/lib/dinit/reboot $@ && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ] && [ $(cat /proc/cmdline | grep -ic "sysvinit") -eq 1 ]; then
					sync  && /usr/lib/sysvinit/init 6 && sleep 3 && /usr/lib/sysvinit/reboot $@ && sync && sleep 3 && /usr/bin/busybox reboot -f;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox reboot -f;
					exit 1
				fi	
		fi
	
fi
