#!/usr/bin/perl -w
# Delete whole swathes of network connections quickly
# Just enter "y" for any you want to get rid of. Deletion 
# doesn't happen until after a final confirmation, at that
# point, you can also select 's' to get it to save a script,
# which you can then review and edit if you made mistakes.
# (c) Phil Carmody 2014-
# Released under CC BY-SA 3.0

$|=1;
my @todel=();
my @todeln=();
open(L, "gconftool -R /system/osso/connectivity/IAP |") or die("gconftool fail");
my ($dir,$type,$name);
while(<L>) {
  if(m,(/system/osso/connectivity/IAP/[-0-9a-f]+):,) {
    $dir=$1; $type = $name = undef;
  }
  elsif(m/\bname = (.*)/) { $name = $1; }
  elsif(m/\btype = ([A-Z]+)/) { $type = $1; } # GPRS or WLAN(_INFRA)
  if(defined($name) and defined($type)) {
    print("Delete $type $name? ");
    if(<> =~ m/^y/i) { push(@todel,$dir); push(@todeln, $name); }
    $name = $type = undef;
  }
}
close(L);
if (@todel) {
  print("Delete ", scalar(@todel), " networks: ", join(", ", @todeln), " (yes/script/No)? ");
  my $resp = <>;
  if($resp =~ m/^y/i) { 
    foreach(@todel) {
      system("gconftool --recursive-unset $_\n");
    }
  }
  if($resp =~ m/^s/i) {
    open(S,"> /tmp/wlanclear.sh");
    foreach(@todel) {                                                    
      print S ("# $todeln[0]\ngconftool --recursive-unset $_\n");
      shift(@todeln);
    }
    close(S);
    print("deletion script written to /tmp/wlanclear.sh");
  }
}
