#!/bin/bash
###
if [ -z "$(command -v wpa_supplicant)" ]; then echo -e "\033[0;31mWPA_Supplicant not found.\033[0m"; exit 8; fi
if [ -z "$(command -v iwconfig)" ]; then echo -e "\033[0;31mwireless-tools not found.\033[0m"; exit 8; fi
if [ -z "$(command -v sed)" ]; then echo -e "\033[0;31msed not found.\033[0m"; exit 8; fi
if [ -z "$(command -v dhcpcd; command -v dhclient)" ]; then echo -e "\033[0;31mdhcp client not found.\033[0m"; exit 8; fi
if ! [ $UID == 0 ]; then echo "Run this script only as root."; exit 1; fi
if [ ! -f /etc/systemd/system/wifiman0@.service ]; then
cat <<EOF > /etc/systemd/system/wifiman0@.service
[Unit]
Description=WifiMan0

[Service]
Type=forking
ExecStart="/etc/wifiman0/.ids/%i/bashconf.sh"

[Install]
WantedBy=multi-user.target
EOF
fi
if [ ! -d /etc/wifiman0 ]; then mkdir /etc/wifiman0; fi
if [ ! -d /etc/wifiman0/.ids ]; then mkdir /etc/wifiman0/.ids; fi
if [ "$(ls -ld /etc/wifiman0 | cut -d' ' -f 1)" != "drwxr-----" ]; then chmod 740 /etc/wifiman0; fi
if ! [ "$(command -v rfkill)" ]; then true; else
if [ -n "$(rfkill -r | grep -Eo "wlan blocked")" ]; then
echo "It seems like your WLAN interface(s) blocked by something. Use rfkill unblock INTERFACE"
cont=1
fi
[[ $cont == 1 ]] && echo "Continue anyway?" && read ques && case $ques in Y*|y*) unset cont ;; *) exit 1 ;; esac; fi
if [ ! -f /etc/wifiman0/.dhcp ]; then
if [ "$(command -v dhcpcd dhclient | wc -l)" == 2 ]; then
	echo -e "\033[0;32mIt seems like you have two DHCP clients.\033[0m \033[0;33mdhcpcd\033[0m and \033[0;33mdhclient\033[0m. Which one to use? (If you do not see the difference, choose \033[0;35mdhclient\033[0m) If you need to change this, execute \033[1;36mwifiman -u CLIENT\033[0m.
1. dhcpcd
2. dhclient"
	read -rp "> " dhcp_choice
	case $dhcp_choice in
	1) future_dhcp=dhcpcd ;; 2) future_dhcp=dhclient ;; dhcp*) future_dhcp=dhcpcd ;; dhcl*) future_dhcp=dhclient ;; *) echo "Error. Unknown DHCP client."; exit 6 ;; esac
	echo -e "You have chosen \033[0;33m${future_dhcp}\033[0m"; echo $future_dhcp > /etc/wifiman0/.dhcp
else
	if [ -n "$(command -v dhcpcd)" ]; then future_dhcp=dhcpcd; echo $future_dhcp > /etc/wifiman0/.dhcp
	else
		if [ -n "$(command -v dhclient)" ]; then future_dhcp=dhclient; echo $future_dhcp > /etc/wifiman0/.dhcp; else echo -e "\033[0;31mDHCP client not found.\033[0m"; exit 8; fi
	fi; fi; fi
dhcp_client="$(cat /etc/wifiman0/.dhcp)"
if [ $1 ]; then INTMOD=1
case "$1" in
-h|--help)
echo "-l/--list to list of available ESSID's
-cp/--create-profile [ "ESSID", interface, "password" ] to creating new Wi-Fi profile using wpa_supplicant
-lp/--list-profiles to view existing profiles
-dp/--delete-profile to delete existing profile
-c/--connect to connect using existing profile
-u/--update to update current dhcp client
-d/--disconnect to disconnect using existing profile
-sp/--show-password to show password from network profile"
;;
-l|--list)
iwlist scan 2>/dev/null | grep ESSID | sed 's/.*://' |  sed -e 's/"//g' && true
;;
-cp|--create-profile)
essid_name=$2
interface_name=$3
essid_password=$4
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ -z "$3" ]; then echo "third argument lost"; exit 2; fi
if [ -z "$4" ]; then essid_password="key_mgmt=NONE"; else essid_password="psk=\"$4\""; fi
if [ "$interface_name" != "$(ls /sys/class/net | grep -x $interface_name)" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$interface_name\033[0m \033[1;31mis not existing interface.\033[0m"; exit 3; fi
if [ -d /etc/wifiman0/"${essid_name}" ]; then REWRITE=1; else
mkdir /etc/wifiman0/"${essid_name}"; fi
cat <<EOF > /etc/wifiman0/"${essid_name}"/wpa.conf
network={
	ssid="${essid_name}"
	${essid_password}
}
EOF
cat <<EOF > /etc/wifiman0/"${essid_name}"/bashconf.sh
#!/bin/bash
wpa_supplicant -c /etc/wifiman0/"${essid_name}"/wpa.conf -i ${interface_name} &
${dhcp_client} ${interface_name} &
EOF
chmod +x /etc/wifiman0/"${essid_name}"/bashconf.sh
if ! [ $REWRITE ]; then
if [ "$(ls /etc/wifiman0 | wc -l)" == 1 ]; then touch /etc/wifiman0/"${essid_name}"/id1+; ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/id1; else
ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))")
touch /etc/wifiman0/"${essid_name}"/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))+")
fi; fi
unset REWRITE
echo -e "\033[1;32mConfiguration file created.\033[0m"
exit 0
;;
-lp|--list-profiles)
ls /etc/wifiman0 | cat
;;
-dp|--delete-profile)
essid_toremove=${@:2}
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_toremove}" != "$(ls /etc/wifiman0 | grep -x "${essid_toremove}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_toremove\033[0m \033[1;31mis not exists.\033[0m"; exit 4; fi
essid_id_toremove="$(ls /etc/wifiman0/"${essid_toremove}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ "$(systemctl is-active wifiman0@"${essid_id_toremove}".service)" == active ]; then systemctl stop wifiman0@"${essid_id_toremove}".service; fi
rm -rf /etc/wifiman0/"${essid_toremove}"
rm /etc/wifiman0/.ids/${essid_id_toremove}
echo -e "\033[1;32mProfile of \033[0;33m${essid_toremove}\033[0m \033[1;32mwas removed.\033[0m"
;;
-c|--connect)
essid_con=${@:2}
if [ -z "${essid_con}" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_con}" != "$(ls /etc/wifiman0 | grep -x "${essid_con}")" ]; then printf "\033[1;31mIt seems like \033[0;33m$essid_con\033[0m \033[1;31mprofile doesn't exists.\033[0m"; exit 4; fi
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo "Turning off other profile"; systemctl stop wifiman0@\*; fi
essid_id_con="$(ls /etc/wifiman0/"${essid_con}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
systemctl start wifiman0@${essid_id_con}.service
if [ $? != 0 ]; then
echo -e "\033[0;31mSomething went wrong. (systemctl status wifiman0@${essid_id_con}.service)\033[0m"; exit 7
else echo "Done"
fi
;;
-u|--update)
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "$(command -v dhcpcd dhclient | wc -l)" == 1 ]; then echo -e "You have only one DHCP client installed - \033[0;33m$(cat /etc/wifiman0/.dhcp)\033[0m"; exit 8; fi
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; exit 4; fi
dhcp_choice=$2
case $dhcp_choice in
dhcpcd)
sed "3s/dhclient/dhcpcd/g" -i /etc/wifiman0/*/bashconf.sh
echo dhcpcd > /etc/wifiman0/.dhcp
echo Done
;;
dhclient)
sed "3s/dhcpcd/dhclient/g" -i /etc/wifiman0/*/bashconf.sh
echo dhclient > /etc/wifiman0/.dhcp
echo Done
;;
*)
echo "Unknown DHCP client. Use dhcpcd or dhclient"
exit 6
;;
esac
;;
-d|--disconnect)
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then
systemctl stop wifiman0@\* &>/dev/null
echo "Done"
else
if [ -n "$(pidof wpa_supplicant)" ]; then killall wpa_supplicant; else
echo -e "\033[1;36mWifiMan doesn't running.\033[0m"; exit 5; fi; fi
;;
-sp|--show-password)
essid_ps=${@:2}
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_ps}" != "$(ls /etc/wifiman0 | grep -x "${essid_ps}")" ]; then printf "\033[1;31mIt seems like \033[0;33m$essid_ps\033[0m \033[1;31mprofile doesn't exists.\033[0m"; exit 4; fi
if [ -n "$(cat /etc/wifiman0/"$essid_ps"/wpa.conf | sed '3!d' | sed "s/.*key_mgmt=//" | grep -x NONE)" ]; then echo "This network doesn't use a password."; else
cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '1d; 2d; $d' | cut -d "\"" -f2- | sed 's/.$//'
fi
;;
*)
echo "Unknown argument"
exit 6
;;
esac
fi
startdialog() {
echo "Choose what to do"
echo "0. List available ESSID's
1. Create Wi-Fi profile using wpa_supplicant
2. List profiles
3. Delete existing profile
4. Connect using existing profile
5. Disconnect using existing profile
6. Get password from profile
7. Change DHCP client
8. Exit"
}
readthis() {
read -rp "> " $*
}
justcases() {
case $uclike in
0)
iwlist scan 2>/dev/null | grep ESSID | sed 's/.*://' |  sed -e 's/"//g' && true
read
startdialog
readthis uclike
justcases
;;
1)
echo "Which interface to configure?(write carefully)"
ls /sys/class/net | sed ''/wlp/s//$(echo -e "\033[0;32mwlp\033[0m")/'' | sed ''/wlan/s//$(echo -e "\033[0;32mwlan\033[0m")/''
readthis interface_name
if ! [ $interface_name ]; then echo "lost argument"; exit 2; fi
if [ "$interface_name" != "$(ls /sys/class/net | grep -x $interface_name)" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$interface_name\033[0m \033[1;31mis not existing interface.\033[0m"
startdialog
readthis uclike
justcases; fi
echo "Which netwotk to configure(ESSID)"
readthis essid_name
if ! [ "$essid_name" ]; then echo "lost argument"; exit 2; fi
echo "What is the password for the network?"
readthis essid_password
if [ -z "$essid_password" ]; then essid_password="key_mgmt=NONE"; else essid_password="psk=\"$essid_password\""; fi
if [ -d /etc/wifiman0/"${essid_name}" ]; then REWRITE=1; else
mkdir /etc/wifiman0/"${essid_name}"; fi
cat <<EOF > /etc/wifiman0/"${essid_name}"/wpa.conf
network={
	ssid="${essid_name}"
	${essid_password}
}
EOF
cat <<EOF > /etc/wifiman0/"${essid_name}"/bashconf.sh
#!/bin/bash
wpa_supplicant -c /etc/wifiman0/"${essid_name}"/wpa.conf -i ${interface_name} &
${dhcp_client} ${interface_name} &
EOF
chmod +x /etc/wifiman0/"${essid_name}"/bashconf.sh
if ! [ $REWRITE ]; then
if [ "$(ls /etc/wifiman0 | wc -l)" == 1 ]; then touch /etc/wifiman0/"${essid_name}"/id1+; ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/id1; else
ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))")
touch /etc/wifiman0/"${essid_name}"/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))+")
fi; fi
unset REWRITE
echo -e "\033[1;32mConfiguration file created.\033[0m"
echo " "
startdialog
readthis uclike
justcases
;;
2)
ls /etc/wifiman0 | cat
echo " "
startdialog
readthis uclike
justcases
;;
3)
echo "Which to remove?"
readthis essid_toremove
if ! [ "$essid_toremove" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_toremove}" != "$(ls /etc/wifiman0 | grep -x "${essid_toremove}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_toremove\033[0m \033[1;31mis not exists.\033[0m"
startdialog
readthis uclike
justcases; fi
essid_id_toremove="$(ls /etc/wifiman0/"${essid_toremove}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ "$(systemctl is-active wifiman0@"${essid_id_toremove}".service)" == active ]; then systemctl stop wifiman0@"${essid_id_toremove}".service; fi
rm -rf /etc/wifiman0/"${essid_toremove}"
rm /etc/wifiman0/.ids/${essid_id_toremove}
echo -e "\033[1;32mProfile of \033[0;33m${essid_toremove}\033[0m \033[1;32mwas removed.\033[0m"
startdialog
readthis uclike
justcases
;;
4)
echo "Which network to connect?"
readthis essid_con
if ! [ "$essid_con" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_con}" != "$(ls /etc/wifiman0 | grep -x "${essid_con}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_con\033[0m \033[1;31mprofile doesn't exists.\033[0m"
startdialog
readthis uclike
justcases; fi
essid_id_con="$(ls /etc/wifiman0/"${essid_con}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo "Turning off other profile"; systemctl stop wifiman0@\*; fi
systemctl start wifiman0@${essid_id_con}.service ||
echo -e "\033[0;31mSomething went wrong. (systemctl status wifiman0@${essid_id_con}.service)\033[0m";
exit 0
;;
5)
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then
systemctl stop wifiman0@\* &>/dev/null
else
if [ -n "$(pidof wpa_supplicant)" ]; then killall wpa_supplicant; else
echo -e "\033[1;36mWifiMan doesn't running.\033[0m"
startdialog
readthis uclike
justcases
fi
fi
;;
6)
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; startdialog; readthis uclike; justcases; fi
echo "Which password to see?"
readthis essid_ps
if ! [ "$essid_ps" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_ps}" != "$(ls /etc/wifiman0 | grep -x "${essid_ps}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_ps\033[0m \033[1;31mprofile doesn't exists.\033[0m"
startdialog; readthis uclike; justcases; fi
if [ "$(cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '3!d' | sed "s/.*key_mgmt=//" | grep -x NONE)" ]; then echo "This network doesn't use a password."; else
cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '1d; 2d; $d' | cut -d "\"" -f2- | sed 's/.$//'
read
startdialog
readthis uclike
justcases
fi
;;
7)
if [ "$(command -v dhcpcd dhclient | wc -l)" == 1 ]; then echo -e "You have only one DHCP client installed - \033[0;33m$(cat /etc/wifiman0/.dhcp)\033[0m\n"; startdialog; readthis uclike; justcases; fi
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; startdialog; readthis uclike; justcases; else
echo -e "Which DHCP client to use? (\033[0;33mdhcpcd\033[0m or \033[0;33mdhclient\033[0m)"
readthis dhcp_choice
case $dhcp_choice in
dhcpcd)
sed "3s/dhclient/dhcpcd/g" -i /etc/wifiman0/*/bashconf.sh
echo dhcpcd > /etc/wifiman0/.dhcp
echo -e "\033[1;32mDone\033[0m"
echo " "; startdialog; readthis uclike; justcases
;;
dhclient)
sed "3s/dhcpcd/dhclient/g" -i /etc/wifiman0/*/bashconf.sh
echo dhclient > /etc/wifiman0/.dhcp
echo -e "\033[1;32mDone\033[0m"
echo " "; startdialog; readthis uclike; justcases
;;
*)
echo "Unknown DHCP client. Use dhcpcd or dhclient"
echo " "
startdialog
readthis uclike
justcases
;;
esac
fi
;;
8)
exit 0
;;
*)
echo "Uknown argument"
startdialog
readthis uclike
justcases
;;
esac
}
if [ ! $INTMOD ]; then
if [ "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo -e "\033[1;33mCurrently, network is working through WifiMan\033[0m"; fi
startdialog
readthis uclike
justcases
fi
