#!/bin/bash
while getopts n:p: option
do
  case "${option}"
  in
  n) NETWORK_NAME=${OPTARG};;
  p) PASSWORD=${OPTARG};
  esac
done


INTERFACE=wlan0

networks=$(wpa_cli -i ${INTERFACE} list_networks | tail -n +2 | grep -o '^\S*')
for net in $networks
do
  wpa_cli -i wlan0 remove_network $net
done

network=$(wpa_cli -i ${INTERFACE} add_network)

wpa_cli -i ${INTERFACE} set_network ${network} ssid '"'$NETWORK_NAME'"'
wpa_cli -i ${INTERFACE} set_network ${network} psk '"'$PASSWORD'"'
wpa_cli -i ${INTERFACE} enable_network ${network}

