# !/bin/bash
START_DIR=$(pwd)
SCRIPT_DIR_TMP=$(dirname "$0")
SCRIPT_DIR=$(cd "${SCRIPT_DIR_TMP}"; pwd)
cd "${SCRIPT_DIR}"
i=0
while [ $i -lt 3 ]
do
	stty -echo
	read -p "请输入密码(Mac登陆密码):" password
	stty echo
	echo ""
	
	printf "%s\n" $password | sudo -S echo "密码正确"	 2 > /dev/null  1> /dev/null
	if [ $? -ne 0 ]; then
		let i++
	else
	   echo "密码正确"	
		i=10
	fi
done

if [ $i -lt 4 ]; then
    echo "密码3次输入错误"
	exit 1
fi
	

	# ----------------------------------------------------------------------------------------------------------------------
	# 打印一行,宽度为INFO_LINECOUNT, $3......$3$2
	# ----------------------------------------------------------------------------------------------------------------------
	function printEndLine () {
	    local preCount_c=$(expr $(echo "${1}" | wc -c) - 1)
	    local preCount_m=$(expr $(echo "${1}" | wc -m) - 1)
	    local preCount_t=$(expr ${preCount_c} - ${preCount_m})
	    local preCount=$(expr ${preCount_c} - ${preCount_t} / 2)

	    local postCount_c=$(expr $(echo "${2}" | wc -c) - 1)
	    local postCount_m=$(expr $(echo "${2}" | wc -m) - 1)
	    local postCount_t=$(expr ${postCount_c} - ${postCount_m})
	    local postCount=$(expr ${postCount_c} - ${postCount_t} / 2)

	    local midCount=$(expr 80 - ${preCount} - ${postCount})
	    local midChar=" "
	    if [ "X${3}" = "X" ];then
	        midChar=" "
	    else
	        midChar=${3:0:1}
	    fi  

	    if [ ${midCount} -lt 0 ];then
	        printf "%s\n" "${2}"
	    else
	        for (( ; midCount > 0; midCount--))
	        do
	            printf "${midChar}"
	        done 
	        printf "%s\n" "${2}"
	    fi

	}

if [ -d "/Library/Scripts/StartWiFi/" ];then
  sudo rm -r -f /Library/Scripts/StartWiFi/
fi
    printf "创建目录//Library/Scripts/StartWiFi/"
    sudo mkdir -p /Library/Scripts/StartWiFi/
    if [ $? -ne 0 ]; then
        printEndLine "创建目录/Library/Scripts/StartWiFi/" "【失败】" "."
        exit 1
    else
        printEndLine "创建目录/Library/Scripts/StartWiFi/" "【成功】" "."
    fi

printf "拷贝文件 AppleIntelWiFi.kext -> /Library/Scripts/StartWiFi/"
sudo cp -r ./package/AppleIntelWiFi.kext /Library/Scripts/StartWiFi/
if [ $? -ne 0 ]; then
    printEndLine "拷贝文件 AppleIntelWiFi.kext -> /Library/Scripts/StartWiFi/" "【失败】" "."
    exit 1
else
    printEndLine "拷贝文件 AppleIntelWiFi.kext -> /Library/Scripts/StartWiFi/" "【成功】" "."
fi

printf "拷贝文件 StartWiFi.sh -> /Library/Scripts/StartWiFi/"
( \
       sudo cp -f package/StartWiFi.sh /Library/Scripts/StartWiFi/StartWiFi.sh \
	&& sudo sed -i '' "s/rootpassword/$password/g"  /Library/Scripts/StartWiFi/StartWiFi.sh \
    && sudo chmod 755 /Library/Scripts/StartWiFi/StartWiFi.sh \
    && sudo chown root:wheel /Library/Scripts/StartWiFi/StartWiFi.sh \
)
if [ $? -ne 0 ]; then
    printEndLine "拷贝文件 StartWiFi.sh -> /Library/Scripts/StartWiFi/" "【失败】" "."
    exit 1
else
    printEndLine "拷贝文件 StartWiFi.sh -> /Library/Scripts/StartWiFi/" "【成功】" "."
fi

printf "拷贝文件 self.laozhiang.startwifi.plist -> /Library/LaunchDaemons/"
( \
       sudo cp -f package/self.laozhiang.startwifi.plist /Library/LaunchDaemons/ \
    && sudo chmod 755 /Library/LaunchDaemons/self.laozhiang.startwifi.plist \
)
if [ $? -ne 0 ]; then
    printEndLine "拷贝文件 self.laozhiang.startwifi.plist -> /Library/LaunchDaemons/" "【失败】" "."
    exit 1
else
    printEndLine "拷贝文件 self.laozhiang.startwifi.plist -> /Library/LaunchDaemons/" "【成功】" "."
fi

if [ -d "/usr/local/StartWiFi/" ];then
sudo rm -r -f  /usr/local/StartWiFi/
fi
    printf "创建目录/usr/local/StartWiFi/"
    sudo mkdir -p /usr/local/StartWiFi/
    if [ $? -ne 0 ]; then
        printEndLine "创建目录/usr/local/StartWiFi/" "【失败】" "."
        exit 1
    else
        printEndLine "创建目录/usr/local/StartWiFi/" "【成功】" "."
    fi


printf "拷贝文件到/usr/local/StartWiFi/"
( \
       sudo cp -f package/StartWiFi-A.sh /usr/local/StartWiFi/ \
	&& sudo sed -i '' "s/rootpassword/$password/g"  /usr/local/StartWiFi/StartWiFi-A.sh \
    && sudo chmod 777 /usr/local/StartWiFi/StartWiFi-A.sh \
)
if [ $? -ne 0 ]; then
    printEndLine "拷贝文件到/usr/local/StartWiFi/" "【失败】" "."
    exit 1
else
    printEndLine "拷贝文件到/usr/local/StartWiFi/" "【成功】" "."
fi


if [ ! -d "/Users/Shared/MenubarTools/WiFiHelper/" ];then
    printf "创建目录/Users/Shared/MenubarTools/WiFiHelper/"
    sudo mkdir -p /Users/Shared/MenubarTools/WiFiHelper/
    if [ $? -ne 0 ]; then
        printEndLine "创建目录/Users/Shared/MenubarTools/WiFiHelper/" "【失败】" "."
        exit 1
    else
        printEndLine "创建目录/Users/Shared/MenubarTools/WiFiHelper/" "【成功】" "."
    fi
	sudo chmod 777 /Users/Shared/MenubarTools/
	sudo chmod 777 /Users/Shared/MenubarTools/WiFiHelper/
fi
	sudo chmod 777 /Users/Shared/MenubarTools/
	sudo chmod 777 /Users/Shared/MenubarTools/WiFiHelper/

printf "拷贝文件到/Users/Shared/MenubarTools/"
( \
       cp -f package/IntelWiFiHelper.1d.sh /Users/Shared/MenubarTools/ \
    && chmod 777  /Users/Shared/MenubarTools/IntelWiFiHelper.1d.sh \
    && cp -f package/ConnectTempWiFi.sh /Users/Shared/MenubarTools/WiFiHelper/ \
    && chmod 777  /Users/Shared/MenubarTools/WiFiHelper/ConnectTempWiFi.sh \
    && cp -f package/SetWifiInterface.sh /Users/Shared/MenubarTools/WiFiHelper/ \
    && chmod 777  /Users/Shared/MenubarTools/WiFiHelper/SetWifiInterface.sh \
    && cp -f package/SwitchWiFiStat.sh /Users/Shared/MenubarTools/WiFiHelper/ \
    && chmod 777  /Users/Shared/MenubarTools/WiFiHelper/SwitchWiFiStat.sh \
    && cp -f package/WiFiInfo.plist /Users/Shared/MenubarTools/WiFiHelper/ \
    && chmod 666  /Users/Shared/MenubarTools/WiFiHelper/WiFiInfo.plist \
)
if [ $? -ne 0 ]; then
    printEndLine "拷贝文件到/Users/Shared/MenubarTools/" "【失败】" "."
    exit 1
else
    printEndLine "拷贝文件到/Users/Shared/MenubarTools/" "【成功】" "."
fi

echo ""
echo ""
echo ""
echo "请将\"BitBar.app\" 拷贝到应用程序目录目录"
echo "运行启动BitBar.app"
echo "选择Plugin目录为 /Users/Shared/MenubarTools/"
echo "在Bitbar弹出菜单中选择\"Preferences -> Open at Login\""
echo ""
echo "..........恭喜，安装成功, 重启生效.........."
exit 0


