#!/bin/bash

password=rootpassword

nvram  WiFi-SSID 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
    exit 1
fi
nvram  WiFi-PW 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
    exit 1
fi

WIFI_SSID=$(nvram  WiFi-SSID | sed -e 's/WiFi-SSID	//g'|sed -e 's/%00$//g')

WIFI_PASSWORD=$(nvram  WiFi-PW | sed -e 's/WiFi-PW	//g'|sed -e 's/%00$//g')
if [ "X${WIFI_SSID}" == "X" ];then
	exit 1
fi
if [[ -d "/tmp/AppleIntelWiFi.kext" ]];then
   echo "$password" | sudo -S  rm -r /tmp/AppleIntelWiFi.kext
fi

echo "$password" | sudo -S cp -r "/Library/Scripts/StartWiFi/AppleIntelWiFi.kext" /tmp
echo "$password" | sudo -S /usr/libexec/PlistBuddy -c "Set :IOKitPersonalities:AppleIntelWiFi:IFConfig:NWID \"${WIFI_SSID}\"" /tmp/AppleIntelWiFi.kext/Contents/Info.plist
echo "$password" | sudo -S /usr/libexec/PlistBuddy -c "Set :IOKitPersonalities:AppleIntelWiFi:IFConfig:WPAKEY \"${WIFI_PASSWORD}\"" /tmp/AppleIntelWiFi.kext/Contents/Info.plist
if [ "X${WIFI_PASSWORD}" == "X" ];then
	echo "$password" | sudo -S /usr/libexec/PlistBuddy -c "Set :IOKitPersonalities:AppleIntelWiFi:IFConfig:WPA/WPA2 false" /tmp/AppleIntelWiFi.kext/Contents/Info.plist
else
	echo "$password" | sudo -S /usr/libexec/PlistBuddy -c "Set :IOKitPersonalities:AppleIntelWiFi:IFConfig:WPA/WPA2 true" /tmp/AppleIntelWiFi.kext/Contents/Info.plist
fi



echo "$password" | sudo -S chmod -R 755 /tmp/AppleIntelWiFi.kext
echo "$password" | sudo -S chmod 644 /tmp/AppleIntelWiFi.kext/Contents/Info.plist
echo "$password" | sudo -S chmod 644 /tmp/AppleIntelWiFi.kext/Contents/_CodeSignature/CodeResources

echo "$password" | sudo -S chown -R root:wheel /tmp/AppleIntelWiFi.kext
echo "$password" | sudo -S xattr -c -r /tmp/AppleIntelWiFi.kext
echo "$password" | sudo -S codesign --force --deep --sign - /tmp/AppleIntelWiFi.kext
echo "$password" | sudo -S kextutil -q /tmp/AppleIntelWiFi.kext