#!/system/bin/busybox sh

start() {
	if ! busybox start-stop-daemon -Ktp /var/run/ftp.pid &> /dev/null; then
		echo -n 'Starting FTP Server... '
		busybox start-stop-daemon -Sbx /system/bin/busybox -mp /var/run/ftp.pid -- tcpsvd -vE 0.0.0.0 21 busybox ftpd -w /mnt/sdcard
		if busybox start-stop-daemon -Ktp /var/run/ftp.pid &> /dev/null; then
			echo 'OK'
		else
			echo 'ERROR'
		fi
	else
		echo 'Error: FTP Server is already started'
		return 1
	fi
}

stop() {
	if busybox start-stop-daemon -Ktp /var/run/ftp.pid &> /dev/null; then
		echo -n 'Stopping FTP Server... '
		busybox start-stop-daemon -Kp /var/run/ftp.pid
		rm /var/run/ftp.pid
	else
		echo "Error: FTP Server isn't started"
		return 1
	fi
}

usage() {
	echo 'usage: ftp <start|stop|restart>'
}

case "$@" in
	start) start;;
	stop) stop;;
	restart) stop; start;;
	*) usage; exit 1;;
esac
