;eMule Update Script
;Auther Ek9ay+ ek9ay9@gmail.com
;Language Chinese


!define VERSION '0.11'
;--------------------------------
;Include Modern UI

  !include "MUI2.nsh"
  !include "Sections.nsh"
  !include "InstallOptions.nsh"
  !include "WinMessages.nsh"
  !include "WordFunc.nsh"

!ifndef VERSION
  !define VERSION 'anonymous-build'
!endif
;--------------------------------

  Var CD
  Var DLPURL
  Var IPFILTERURL
  Var NODESURL
  Var IP2COUNTRY
  Var CUSTOMFLAG
  Var HWND
  Var ERRFLAG
;--------------------------------
;General

  ;Name and file
  Name "eMule 更新工具 ${VERSION}"
  OutFile "Updatefiles-${VERSION}.exe"

  BrandingText "By Ek9ay+ ${__TIMESTAMP__}"

  ;Request application privileges for Windows Vista
  RequestExecutionLevel user
  ShowInstDetails show

;--------------------------------
;Interface Settings

  InstallButtonText "更新"
  Caption "eMule 更新工具"
  !define MUI_HEADERIMAGE_RIGHT
  !define MUI_ABORTWARNING_TEXT "真的要退出更新程序吗?"
  !define MUI_COMPONENTSPAGE_TEXT_TOP "勾选你想要更新的组件，并解除勾选你不希望更新的组件。单击[下一步(N)]继续."
  !define MUI_UI "media\modern.exe"
  !define MUI_ABORTWARNING
  !define MUI_HEADERIMAGE
  !define MUI_HEADERIMAGE_BITMAP 'media\Head.bmp'
  !define MUI_WELCOMEFINISHPAGE_BITMAP 'media\Left.bmp'
  !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"

;--------------------------------
;Pages

  !Define MUI_PAGE_HEADER_TEXT "选择更新的组件"
  !Define MUI_PAGE_HEADER_SUBTEXT "选择你想要更新哪些组件。"
  !Define MUI_PAGE_CUSTOMFUNCTION_LEAVE Components_Leave
  !insertmacro MUI_PAGE_COMPONENTS
  Page custom PageCus_Creat PageCus_Leave
  !Define MUI_PAGE_HEADER_TEXT "正在更新"
  !Define MUI_PAGE_HEADER_SUBTEXT "正在更新您选择的组件，请稍候..."
  !insertmacro MUI_PAGE_INSTFILES
  !define MUI_FINISHPAGE_TITLE "恭喜，已完成更新！"
  !define MUI_FINISHPAGE_TEXT "“eMule 更新工具”已完成更新。单击[完成(F)]退出程序。"
  !define MUI_FINISHPAGE_RUN "$CD\emule.exe"
  !define MUI_FINISHPAGE_RUN_TEXT "现在运行eMule"
  !define MUI_FINISHPAGE_LINK "下载最新版本"
  !define MUI_FINISHPAGE_LINK_LOCATION "http://ek9ay.heliohost.org/?p=231"
  !insertmacro MUI_PAGE_FINISH

;--------------------------------
;Languages

  !insertmacro MUI_LANGUAGE "SimpChinese"

;--------------------------------
;Installer Sections

Section ""

  SectionIn RO

  DetailPrint "读取设置..."
  ReadINIStr $DLPURL '$CD\UPConfig.ini' Update DLPURL
  ReadINIStr $IPFILTERURL '$CD\UPConfig.ini' Update IPFILTERURL
  ReadINIStr $NODESURL '$CD\UPConfig.ini' Update NODESURL
  ReadINIStr $IP2COUNTRY '$CD\UPConfig.ini' Update IP2COUNTRY

  DetailPrint "备份旧文件..."
  CopyFiles /SILENT '$CD\antiLeech.dll' '$CD\UPBackup\'
  CopyFiles /SILENT '$CD\config\ipfilter.dat' '$CD\UPBackup\'
  CopyFiles /SILENT '$CD\config\nodes.dat' '$CD\UPBackup\'
  CopyFiles /SILENT '$CD\config\ip-to-country.csv' '$CD\UPBackup\'

SectionEnd



SectionGroup /e "更新(联机)"

  Section "更新DLP" SecUpdateDLP

    DetailPrint "开始下载DLP..."
    ${WordFind} $DLPURL "/" '-1' $R0
    InetLoad::load /POPUP "下载DLP..." "$DLPURL" "$CD\antiLeech.dll.new"
    Pop $0
    StrCmp $0 "OK" +3
    DetailPrint "下载DLP出错"
    StrCpy $ERRFLAG "1"
    ${WordFind} $R0 "." '-1' $R1
    StrCmp $R1 'zip' 0 +4
    DetailPrint "解压antiLeech.dll.zip..."
    ZipDLL::extractall "$CD\$R0" "$CD"
    Delete "$CD\$R0"

  SectionEnd

  Section "更新ipfilter.dat" SecUpdateipfilter

    DetailPrint "开始下载ipfilter.dat..."
    ${WordFind} $IPFILTERURL "/" '-1' $R0
    Delete '$CD\config\ipfilter.dat'
    InetLoad::load /POPUP "下载ipfilter.dat..." $IPFILTERURL "$CD\Config\ipfilter.zip"
    Pop $0
    StrCmp $0 "OK" +3
    DetailPrint "下载ipfilter.dat出错"
    StrCpy $ERRFLAG "1"
    ${WordFind} $R0 "." '-1' $R1
    StrCmp $R1 'zip' 0 +4
    DetailPrint "解压ipfilter.dat.zip..."
    ZipDLL::extractall "$CD\Config\$R0" "$CD\Config"
    Delete "$CD\config\$R0"

  SectionEnd
  
  Section "更新nodes.dat" SecUpdatenodes
    DetailPrint "开始下载nodes.dat..."
    ${WordFind} $NODESURL "/" '-1' $R0
    Delete "$INSTDIR\Config\nodes.dat"
    InetLoad::load /POPUP "下载nodes.dat..." $NODESURL "$CD\Config\nodes.dat"
    Pop $0
    StrCmp $0 "OK" +3
    DetailPrint "下载nodes.dat出错"
    StrCpy $ERRFLAG "1"
    ${WordFind} $R0 "." '-1' $R1
    StrCmp $R1 'zip' 0 +4
    DetailPrint "解压nodes.dat.zip..."
    ZipDLL::extractall "$CD\Config\$R0" "$CD\Config"
    Delete "$CD\config\$R0"

  SectionEnd

  Section "更新ip-to-country.csv" SecUpdateiptocountry

    DetailPrint "开始下载ip-to-country.csv..."
    ${WordFind} $IP2COUNTRY "/" '-1' $R0
    Delete '$CD\ip-to-country.csv'
    InetLoad::load /POPUP "下载ip-to-country.csv..." $IP2COUNTRY "$CD\Config\$R0"
    Pop $0
    StrCmp $0 "OK" +3
    DetailPrint "下载ip-to-country.csv出错"
    StrCpy $ERRFLAG "1"
    ${WordFind} $R0 "." '-1' $R1
    StrCmp $R1 'zip' 0 +4
    DetailPrint "解压ip-to-country.csv.zip..."
    ZipDLL::extractall "$CD\Config\$R0" "$CD\Config"
    Delete "$CD\config\$R0"

  SectionEnd

SectionGroupEnd

SectionGroup /e "选项" 

  Section /o "自定义更新地址" SecCustomURL

  StrCmp $ERRFLAG '1' 0 +2
    MessageBox MB_OK "更新时有错误发生"

  SectionEnd

SectionGroupEnd
;--------------------------------
;描述

  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecUpdateiptocountry} "更新到最新的IP地区数据库文件ip-to-country.csv"
    !insertmacro MUI_DESCRIPTION_TEXT ${SecUpdateipfilter} "更新到最新的IP过滤数据库文件ipfilter.dat"
    !insertmacro MUI_DESCRIPTION_TEXT ${SecUpdatenodes} "更新到最新的Kad节点文件nodes.dat，用来连接Kad网络，连不上Kad请更新此项"
    !insertmacro MUI_DESCRIPTION_TEXT ${SecUpdateDLP} "更新到最新的DLP库更新文件antiLeech.dll.new"
    !insertmacro MUI_DESCRIPTION_TEXT ${SecCustomURL} "自定义各项更新地址"
  !insertmacro MUI_FUNCTION_DESCRIPTION_END


;-------------------------
;Functions

Function .onInit

    System::Call 'kernel32::GetCurrentDirectory(i 1024,t .R0)'
    StrCpy $CD $R0
      IfFileExists $CD\UPBackup\*.* +2 0
        CreateDirectory $CD\UPBackup
      IfFileExists $CD\eMule.exe +3 0
        MessageBox MB_OK "未发现eMule.exe请把本程序放在电骡目录中执行，单击确定退出"
        Abort
      IfFileExists $CD\UPConfig.ini +5 0
      WriteINIStr $CD\UPConfig.ini Update DLPURL "http://emule-xtreme.googlecode.com/files/antiLeech.dll.new"
      WriteINIStr $CD\UPConfig.ini Update IPFILTERURL "http://ipfilter-emule.googlecode.com/files/ipfilter.zip"
      WriteINIStr $CD\UPConfig.ini Update NODESURL "http://upd.emule-security.net/nodes.dat"
      WriteINIStr $CD\UPConfig.ini Update IP2COUNTRY "http://ip2c.googlecode.com/files/ip2c-zhcn-short.zip"
    !insertmacro INSTALLOPTIONS_EXTRACT "PageCustom.ini"

FunctionEnd

Function Components_Leave

  SectionGetFlags ${SecCustomURL} $0
  StrCmp $0 '1' 0 +3
    StrCpy $CUSTOMFLAG 1
    Goto +2
    StrCpy $CUSTOMFLAG "0"
;  MessageBox MB_OK $CUSTOMFLAG

FunctionEnd


Function PageCus_Creat

  StrCmp $CUSTOMFLAG "0" 0 +2
    Return
  ReadINIStr $DLPURL $CD\UPConfig.ini Update DLPURL
  ReadINIStr $IPFILTERURL $CD\UPConfig.ini Update IPFILTERURL
  ReadINIStr $NODESURL $CD\UPConfig.ini Update NODESURL
  ReadINIStr $IP2COUNTRY $CD\UPConfig.ini Update IP2COUNTRY
  
  !insertmacro INSTALLOPTIONS_WRITE "PageCustom.ini" "Field 5" "State" $DLPURL
  !insertmacro INSTALLOPTIONS_WRITE "PageCustom.ini" "Field 6" "State" $IPFILTERURL
  !insertmacro INSTALLOPTIONS_WRITE "PageCustom.ini" "Field 7" "State" $NODESURL
  !insertmacro INSTALLOPTIONS_WRITE "PageCustom.ini" "Field 8" "State" $IP2COUNTRY
  
  !insertmacro MUI_HEADER_TEXT "设置自定义更新地址" "请在这里设置自定义更新地址。"

  !insertmacro INSTALLOPTIONS_INITDIALOG "PageCustom.ini"
  Pop $HWND

  !insertmacro INSTALLOPTIONS_SHOW

FunctionEnd

Function PageCus_Leave

	!insertmacro INSTALLOPTIONS_READ $0 "PageCustom.ini" "Settings" "State"
	StrCmp $0 10 Default
  !insertmacro INSTALLOPTIONS_READ $DLPURL "PageCustom.ini" "Field 5" "State"
  !insertmacro INSTALLOPTIONS_READ $IPFILTERURL "PageCustom.ini" "Field 6" "State"
  !insertmacro INSTALLOPTIONS_READ $NODESURL "PageCustom.ini" "Field 7" "State"
  !insertmacro INSTALLOPTIONS_READ $IP2COUNTRY "PageCustom.ini" "Field 8" "State"

  WriteINIStr $CD\UPConfig.ini Update DLPURL $DLPURL
  WriteINIStr $CD\UPConfig.ini Update IPFILTERURL $IPFILTERURL
  WriteINIStr $CD\UPConfig.ini Update NODESURL $NODESURL
  WriteINIStr $CD\UPConfig.ini Update IP2COUNTRY $IP2COUNTRY
  Goto CusEnd
  
  Default:
	GetDlgItem $0 $HWND 1204
	SendMessage $0 ${WM_SETTEXT} 1 "STR:http://emule-xtreme.googlecode.com/files/antiLeech.dll.new"
	GetDlgItem $0 $HWND 1205
	SendMessage $0 ${WM_SETTEXT} 1 "STR:http://ipfilter-emule.googlecode.com/files/ipfilter.zip"
	GetDlgItem $0 $HWND 1206
	SendMessage $0 ${WM_SETTEXT} 1 "STR:http://upd.emule-security.net/nodes.dat"
	GetDlgItem $0 $HWND 1207
	SendMessage $0 ${WM_SETTEXT} 1 "STR:http://ip2c.googlecode.com/files/ip2c-zhcn-short.zip"

  EnableWindow $0 1

  Abort
  CusEnd:
  
FunctionEnd
