#!/extra/busybox ash
case $1 in
        start )

		if [ -f /tmp/dropbear.pid ]; then
			echo "Dropbear is already running!"
			exit
		fi

		# Setup and start dropbear
			echo "*** Starting Dropbear..."

			if [ ! -f /etc/passwd ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "root:x:0:0::/:/bin/ash" > /etc/passwd
			fi

			if [ ! -f /etc/shadow ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "root::14531:0:99999:7:::" > /etc/shadow
			fi

			if [ ! -f /etc/group ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "root:x:0:" > /etc/group
			fi

			if [ ! -f /etc/gshadow ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "root:!::" > /etc/gshadow
			fi

			if [ ! -f /etc/shells ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "/bin/ash" > /etc/shells
			fi

			if [ ! -f /etc/profile ]; then
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				echo "PATH=\"/sbin:/system/sbin/:/system/bin:/system/xbin:/bin:/extra:/data/local:/data/local/bin\"" > /etc/profile
				echo "HOME=\"/\"" >> /etc/profile
				echo "PS1=\"\[\033]0;root@moment:\w\007\]\[\033[01;31m\]root@moment \[\033[01;34m\]\W \\$ \[\033\[00m\]\"" >> /etc/profile
				echo "export PATH" >> /etc/profile
				echo "export HOME" >> /etc/profile
				echo "export PS1" >> /etc/profile
				echo " " >> /etc/profile
				echo "set -o vi" >> /etc/profile
				echo " " >> /etc/profile
				echo "alias ls='/extra/busybox ls -a --color'" >> /etc/profile
			fi

			if [ ! -d /system/etc/dropbear ]; then
				echo "*** Creating Dropbear Configuration Folder..."
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				/extra/busybox mkdir /system/etc/dropbear
			fi
	
			if [ ! -f /system/etc/dropbear/rsa_host_key ]; then
				echo "*** Generating Dropbear RSA Key..."
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				/extra/dropbearkey -t rsa -f /system/etc/dropbear/rsa_host_key
			fi

			if [ ! -f /system/etc/dropbear/dss_host_key ]; then
				echo "*** Generating Dropbear DSS Key..."
				if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 0 ]; then
					/extra/remount rw
				fi
				/extra/dropbearkey -t dss -f /system/etc/dropbear/dss_host_key
			fi

			if [ $(/system/bin/mount | /extra/busybox grep stl5 | /extra/busybox grep rw | /extra/busybox wc -l) = 1 ]; then
				/extra/remount ro
			fi

			/extra/dropbear -A root -N root -C momentdev -U 0 -G 0 -d /system/etc/dropbear/dss_host_key -r /system/etc/dropbear/rsa_host_key -p 2200 -P /tmp/dropbear.pid;;
        stop )
		if [ ! -f /tmp/dropbear.pid ]; then
			echo "Dropbear is not running..."
			exit
		fi

		if [ -f /tmp/dropbear.pid ]; then
			echo "*** Stopping Dropbear..."
			/extra/busybox kill `cat /tmp/dropbear.pid`
		fi;;

        * )
                echo "Usage: $0 start|stop";;
esac



