#!/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
					/usr/lib/runit/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "systemd") -eq 1 ]; then
					/usr/lib/systemd/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ]; then
					/usr/lib/sysvinit/runlevel $@;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox runlevel -f;
					exit 1
				fi
		else
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					/usr/lib/runit/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "systemd") -eq 1 ]; then
					/usr/lib/systemd/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ]; then
					/usr/lib/sysvinit/runlevel $@;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox runlevel -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
					/usr/lib/runit/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "systemd") -eq 1 ]; then
					/usr/lib/systemd/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ]; then
					/usr/lib/sysvinit/runlevel $@;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox runlevel -f;
					exit 1
				fi
		else
				if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
					/usr/lib/runit/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "systemd") -eq 1 ]; then
					/usr/lib/systemd/runlevel $@;
					exit 0
				elif [ $(ps -p1 | grep -ic "init") -eq 1 ]; then
					/usr/lib/sysvinit/runlevel $@;
					exit 0
				else 
					sync && sleep 5 && /usr/bin/busybox runlevel -f;
					exit 1
				fi	
		fi
	
fi
