#!/bin/sh
#
# SliTaz Config - A tool with all SliTaz Ncurses configs in one place for
# text mode systems (server, ARM devices)
#
# Copyright (C) 2014-2015 SliTaz ARM - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
#

. /lib/libtaz.sh
export TEXTDOMAIN='slitaz-tools' #i18n

check_root

title="{ $(_ 'SliTaz Config') }"
about='/usr/share/doc/slitaz/post-install.txt'
tmpdir="/tmp/$(basename $0)"
tmp="$tmpdir/$$"
height='20'
width='72'


# Use a tmp directory

mkdir -p $tmpdir

quit() {
	rm -rf $tmpdir; exit 0
}


#
# GUI Functions
#


# Coded for the ARM first boot settings

about_post_install() {
	dialog --cr-wrap \
		--title "{ $(_ 'Post Installation') }" \
		--exit-label "$(_ 'Continue')" \
		--textbox "$about" $height $width
}


# Set root passwd

root_passwd() {
	dialog --title "{ $(_ 'Root Password') }" --colors \
		--inputbox "\n$(_ 'Enter new password for %s' '\Zb\Z1root')" \
		12 $width 2>$tmp
	passwd=$(cat $tmp)
	[ -z "$passwd" ] && return 0
	echo "root:$passwd" | chpasswd --md5 >/dev/null
}


# Add a new user

add_user() {
	title2="{ $(_ 'Add User') }"

	dialog --title "$title2" --colors \
		--inputbox "\n$(_ 'Enter login name for the new \Zb\Z4user')" 12 $width 2>$tmp
	user=$(cat $tmp)
	[ -z "$user" ] && return 0

	dialog --title "$title2" --colors \
		--inputbox "\n$(_ 'Enter password for user \Zb\Z4%s' "$user")" 12 $width 2>$tmp
	passwd=$(cat $tmp)
	[ -z "$passwd" ] && return 0

	adduser -D -g "SliTaz User" -G users $user
	echo "$user:$passwd" | chpasswd --md5 >/dev/null

	# User groups
	for group in audio cdrom video tty; do
		addgroup $user $group >/dev/null
	done

	# Make sure system-wide applications.conf is used
	config="/home/$user/.config"
	mkdir -p $config/slitaz
	cp -f /etc/slitaz/applications.conf $config/slitaz

	# Slim default user on post-install
	if [ -f "/etc/slim.conf" ] && [ ! -f "/var/lib/slitaz/post-install" ]; then
		sed -i s"/default_user .*/default_user        $user/" /etc/slim.conf
	fi
}


set_date() {
	clear; newline
	echo "Old date: $(date)"
	rdate -s tick.greyware.com 2>/dev/null
	echo "New date: $(date)"
	sleep 4
}


# Catch ESSIDs and format output for Ncurses Dialog or GTK Yad tree.
# We get the list of networks by Cell and without spaces.
# Usage: detect_wifi --output=gtk (default output to dialog)

detect_wifi() {
	. /etc/network.conf
	ifconfig $WIFI_INTERFACE up

	if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
		IFS=$'\n'; hidden=''
		for i in $(iwlist $WIFI_INTERFACE scan | grep -Eo 'Cell [0-9]*'); do
			scan=$(iwlist $WIFI_INTERFACE scan last | \
				awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
				sed "/$i/,/Cell/ !d")

			essid=$(echo $scan | cut -d '"' -f 2)

			quality=$(echo $scan | grep -Eo 'Quality[:=][^ ]*' | tr -cd '0-9/')
			[ -z "$quality" ] && quality='-----'

			crypto="$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')"

			# Check encryption type
			if echo "$scan" | grep -q WPA*; then
				crypto="WPA"
			fi

			# Connected or not connected...
			if ifconfig | grep -A1 $WIFI_INTERFACE | grep -qF 'inet' && \
				iwconfig $WIFI_INTERFACE | grep ESSID | grep -qwF "$essid"; then
				status="..$(_ 'connected')"
			else
				status=''
			fi

			# Output
			if [ -z "$essid" ]; then
				hidden='yes'
			else
				case "$output" in
					gtk)
						#echo -e "$(_n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
						echo -e "$essid\n$quality\n$crypto\n$status" ;;
					*)
						echo "$essid"
						echo "$(_ 'Quality'):${quality}..$(_ 'Key'):${crypto}${status}" ;;
				esac
			fi
		done
		[ -n "$hidden" ] && echo "$(_ 'hidden.network')" "$(_ 'Connect.to.a.hidden.network')"
	fi
}


# Show message and percentage on the dialog gauge

msg() {
	sleep 1; echo -e "XXX\n$1\n$MSG\nXXX"
}


# Wireless config so users don't have to edit any config files on post
# install to get connected. If the wired connection is used it will auto
# connect with DHCP so no need for a dialog frontend.

wifi_setup() {
	. /etc/network.conf
	dialog \
		--clear --title "$title" \
		--ok-label "$(_ 'Select')" \
		--menu "\n$(_ 'Connect to a Wi-Fi network')" \
		$height $width 14 \
"any" "$(_ 'Connect.to.any.network')" \
$(detect_wifi) 2>$tmp

	# Handle options
	case "$?" in
		1|255) quit ;;
		0) essid=$(cat $tmp) ;;
	esac

	# Connect to hidden network
	if [ "$essid" = "$(_ 'hidden.network')" ]; then
		dialog --title "{ $(_ 'Wi-Fi ESSID') }" \
			--inputbox "\n$(_ 'Enter Wi-Fi access point ESSID (name)')" \
			12 $width 2>$tmp
		essid=$(cat $tmp)
		[ -z "$essid" ] && exit 0

		dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
			--inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4%s' "$essid")" \
			12 $width 2>$tmp
		key=$(cat $tmp)
	else

		# Check if we need to prompt user for an encrypted network
		key=$(iwlist $WIFI_INTERFACE scan last | grep -E 'Cell |Encryption|ESSID' | \
			grep -C1 "$essid" | sed -n 's|.*key:\(.*\)|\1|p')

		if [ "$key" = "on" ]; then
			dialog --title "{ $(_ 'Wi-Fi Password') }" --colors \
				--inputbox "\n$(_ 'Enter Wi-Fi key (password) for \Zb\Z4%s' "$essid")" \
				12 $width 2>$tmp
			key=$(cat $tmp)
			[ -z "$key" ] && exit 0
		fi
	fi

	# Configure connection
	{
		MSG="\n$(_ 'Shutting down network interfaces...')"
		msg 0; stopd network.sh >/dev/null 2>&1
		msg 10

		MSG="\n$(_ 'Configuring: %s...' '/etc/network.conf')"
		msg 20
		# WIFI_KEY_TYPE=any should work for WEP/WPA*
		sed -i \
			-e s"/^WIFI=.*/WIFI=\"yes\"/" \
			-e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
			-e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
			-e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
			/etc/network.conf

		MSG="\n$(_ 'Restarting Wi-Fi interface...')"
		msg 30; startd network.sh >/dev/null 2>&1
		msg 40; msg 50; msg 60; msg 70; msg 80

		MSG="\n$(_ 'Checking connection...')"
		msg 90; sleep 2
		ip="$(ifconfig $WIFI_INTERFACE | fgrep 'inet addr' | sed 's|.*:\([^ ]*\) .*|\1|')"

		if [ -n "$ip" ]; then
			MSG="\n$(_ 'IP address: %s' "$ip")"
		else
			MSG="\n$(_ 'Unable to connect...')"
		fi
		msg 100; sleep 2

	} | dialog --title "{ $(_ 'Wi-Fi Config') }" --gauge "" 8 $width 0
}


# Main Dialog menu

main_box() {
	dialog \
		--clear --title "$title" \
		--ok-label "$(_ 'Exec')" --cancel-label "$(_ 'Quit')" \
		--menu "" $height $width 14 \
"keyboard"    "$(_ 'System keyboard setting')"      \
"locale"      "$(_ 'System language setting')"      \
"wifi-setup"  "$(_ 'Wi-Fi network settings')"       \
"add-user"    "$(_ 'Add a new user')"               \
"root-passwd" "$(_ 'Change root password')"         \
"set-date"    "$(_ 'Set system date from the web')" \
"quit"        "$(_ 'Exit from SliTaz Config')"      2>$tmp

	# Handle options
	opt="$?"
	case "$opt" in
		1|255) quit ;;
	esac

	# Handle actions
	action=$(cat $tmp)
	case "$action" in
		keyboard)    tazkeymap   ;;
		locale)      tazlocale   ;;
		wifi-setup)  wifi_setup  ;;
		add-user)    add_user    ;;
		root-passwd) root_passwd ;;
		set-date)    set_date    ;;
		quit)        quit        ;;
	esac
}


#
# Handle commands
#

case "$1" in
	*_*)
		# Execute functions
		$@ ;;
	*)
		while true; do
			main_box
		done ;;
esac


# Clean exit

#rm -rf ${tmpdir}
exit 0
