#!/bin/sh
#
# A tiny utility to help manage SliTaz R-Pis from an i486 machine. Make
# it clean and fast for users :-). if you're looking for the SliTaz R-Pi 
# distro builder, have a look at sat-rpi.
#
# (C) 2014 SliTaz ARM - BSD License
# AUTHOR: Christophe Lincoln <pankso@slitaz.org> 
#
# TODO:
#
#   GUI box to dl/install SliTaz R-Pi on sdcard (à la tazusb-box)
#   SD card DD backup and restore
#   Setup a R-Pi webboot on any i?86/X64 server/machine
#
#
. /lib/libtaz.sh

pilist="$HOME/.config/slitaz/rpi.list"

usage() {
	cat << EOT

$(boldify "Usage:") $(basename $0) [command] [host]

$(boldify "Commands:")
  netmap    Search and map R-Pis on the wired local network
  pscan     Scan one or all Raspberry Pi hosts open ports
  box       Run the GTK/Yad graphical user interface

EOT
}

ssh_term() {
	xterm -e "echo 'Connecting: $@'; ssh $@ || read"
}

netmap_term() {
	xterm -e "$0 netmap; echo -n 'Press ENTER to continue...'; read"
	$0 box
}

pscan_term() {
	local host="$1"
	echo $0 pscan $host
	xterm -e "$0 pscan $1; echo -n 'Press ENTER to continue...'; read"
	$0 box
}

rpi_list() {
	for host in $(cat $pilist); do
		echo -n "$host!"
	done
	echo "Clean list"
}

rpi_box() {
	count=$(cat $pilist | wc -l)
	logo="/usr/share/pixmaps/raspberrypi.png"
	text="<b>Connect to your local Raspberry Pi devices</b>\nRaspberry Pi: $count\n"
	# Have a config file, store RPi IPs, have button to launch an SSH
	# connection or an sftp connection with gftp.
	yad --form --borders=4 \
		--window-icon="$logo" \
		--title="SliTaz RPi" --center \
		--image="$logo" --image-on-top \
		--text "$text" --width=520 --height=240 \
		--button="Web admin!gtk-properties:4" \
		--button="Netmap!find:2" \
		--button="Connect!insert-link:0" \
		--button="Cancel!gtk-close:1" \
		--field="$(gettext "Raspberry Pi host:")":CB \
		--field="$(gettext "New device IP:")" \
		--field="$(gettext "User name:")" \
		"$(rpi_list)" "" "$USER"
}

case "$1" in
	netmap)
		# MAC address works for wired raspberry R-Pis
		# http://hwaddress.com/?q=raspberry
		newline && colorize 35 "Raspberry Pi wired LAN map"
		separator
		arp -a | grep -i "b8:27:eb" | awk '{print $2}' | while read line
		do
			ip=$(echo "$line" | tr -d '()')
			ssh="$(colorize 31 OFF)"
			http="$ssh"
			if pscan ${ip} -p 22 -P 22 | fgrep 'ssh' | fgrep -q 'open'; then
				ssh="$(colorize 32 ON)"
			fi
			if pscan ${ip} -p 81 -P 81 | fgrep 'http' | fgrep -q 'open'; then
				http="$(colorize 32 ON)"
			fi
			echo -n "IP: $ip $(indent 30 "SSH: $ssh")"
			indent 46 "HTTP: $http"
		done
		separator
		count=$(arp -a | grep -i "b8:27:eb" | wc -l)
		echo -n "Raspberry Pi found: "; boldify "$count"
		newline ;;
	
	pscan)
		if [ "$2" ]; then
			pscan ${2} && exit 0
		fi
		arp -a | grep -i "b8:27:eb" | awk '{print $2}' | while read line
		do
			ip=$(echo "$line" | tr -d '()')
			newline
			echo "$(colorize 35 'Raspberry Pi Open ports')"
			separator
			pscan ${ip}
			separator; newline
		done ;;
	
	box)
		# Store box results
		main=$(rpi_box)
		action="$?"
		
		host=$(echo $main | cut -d "|" -f 1)
		new=$(echo $main | cut -d "|" -f 2)
		user=$(echo $main | cut -d "|" -f 3)
		
		# New host
		[ "$new" ] && host="$new" && main="$new"
		if [ "$new" ] && ! grep "^$new" ${pilist}; then
			echo "$new" >> ${pilist}
		fi
		
		# Deal with --button actions
		case "$action" in
			0) ssh_term "$user@$host" ;;
			1) exit 0 ;;
			2) netmap_term ;;
			4) tazweb --notoolbar ${host}/adm ;;
			*) continue ;;
		esac
		
		# Deal with $main values
		case "$main" in
			Clean*) 
				echo "Cleaning: $pilist" 
				rm -f ${pilist} && touch ${pilist} 
				$0 box ;;
		esac ;;
	
	backup) 
		# dd an SD card with time stamp
		;;
	
	restore) 
		# Restore a previous backup SD card
		;;
	
	*) usage ;;
	
esac && exit 0
