#!/bin/bash
MAILFILE="Tempfile"

TRYCOUNT=10
while [ TRUE ]; do

  if [ $TRYCOUNT -le 0 ]; then
    echo Error occured.
    exit 1
  fi

  CURRENTIP=$(curl --silent http://checkip.dyndns.org/|grep -Eo '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}')
  STATUS=$?
  sleep 5
  if [ ! $STATUS ]; then
    let TRYCOUNT-=1
  elif [ ! "$CURRENTIP" ]; then
    let TRYCOUNT-=1
  else
    break
  fi

done



touch $MAILFILE
exec 6>&1           # Link file descriptor #6 with stdout.
                    # Saves stdout.
exec > $MAILFILE     # stdout replaced with file $LOGFILE.
PSDDLEN=$(((RANDOM % 9) + 16))
PASS=$( java -jar /opt/utils/pwgen.jar $PSDDLEN )
htpasswd -b /var/auths/squid proxyuser $PASS


echo This is an auto generated mail to notify the password had changed for the proxy server:
echo 這是一封自動發送的電郵通知,密碼經已變更,登入時請使用新的密碼〔於「」內〕:
echo Passward \(enclosed in 「」\):
echo 
echo 「$PASS」
echo 
echo 
echo Additional Information:
echo 附加資訊:
echo 
echo Server Address:
echo 服務器地址:
echo $CURRENTIP
echo 
echo Server port:
echo 服務器端口:
echo 
echo 3128
echo 
echo 
echo 
echo Please be awared that the password will change for each week.
echo 請留意密碼會每星期變更一次。
echo 
echo 
echo Pleas do not reply to this email.
echo 請勿回覆。
echo If you have any questions, please feel free to contact tgckpg@gmail.com
echo 如有任何疑問,請電郵至tgckpg@gmail.com
echo 
echo Regards,
echo 祝 安康
echo Penguin 鵬兄
echo 
echo 
echo Password Generator powered by Moonbird.


while read line
do mutt -s "通知Notice: 密碼已變更New pasword for squid proxy server." -- $line < $MAILFILE
done < /opt/utils/mailListProxy

rm $MAILFILE