#!/system/bin/sh

[ "$(whoami)" != "root" ] && {
    echo "root required"
    exit 1
}
configs=(/data/misc/wifi/WifiConfigStore.xml /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml)
for z in ${configs[@]}; do
    if [ -f $z ]; then
        config=$z
        break
    fi
done

SSID=($(grep 'name="SSID"' $config | sed "s/.*>&quot;//;s/&quot;.*//;s/ /-_-/g"))
PSK=($(grep 'name="PreSharedKey"' $config | sed "s/<null.*/\e[01;30mNONE\e[00;37;40m/;s/.*>&quot;/\e[01;32m/;s/&quot;.*/\e[00;37;40m/;s/ /-_-/g"))

echo "WiFi Password Viewer"
echo "Der_Googler, veez21 @ xda-developers\n"

for i in ${!SSID[@]}; do
    # MMRL does not support ansi colors
    if [ -z $MMRL ]; then
        echo "\e[01;37m${SSID[$i]}\e[00;37;40m - ${PSK[$i]}" | sed "s/-_-/ /g"
    else
        echo "${SSID[$i]} - ${PSK[$i]}" | sed "s/-_-/ /g"
    fi
done
