; ecard_installer.nsi

;--------------------------------

;includes
!include LogicLib.nsh
!include nsDialogs.nsh
!include "Registry.nsh"
!include "Sections.nsh"

; Basic Params
Name "EmailIdeas Ecard"
OutFile "Ecard-Installer9.exe"
InstallDir $DOCUMENTS\Emailideas-eCard
ShowInstDetails show

; Let's generate a date string, so we don't overwrite sig.html
!define /date MyDATESTAMP "%Y%m%d%H%M%S"

;--------------------------------
;Interface Settings

;Modern UI style... uncomment this to use modern UI. Note that my custom pages have to go between insertmacro MUI_PAGE, 
;as the order listed here determines flow in the install process.
  !include "MUI.nsh"
  !define MUI_ICON "${NSISDIR}\Examples\emailideas.ico"
  !define MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Examples\emailideas_left.bmp"
  !define MUI_WELCOMEPAGE_TEXT "this is my text."
  LangString PAGE_TITLE ${LANG_ENGLISH} "Install Code"
  LangString PAGE_SUBTITLE ${LANG_ENGLISH} "Your install code was provided on-screen at the completion of your eCard design,$\nand also emailed to you."

  !insertmacro MUI_PAGE_WELCOME
  Page custom nsDialogsPage nsDialogsPageLeave
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_LANGUAGE "English"

;---------------------------------
; Vars

Var Dialog
Var Label
Var Installcode

;---------------------------------
; Functions

Function .onInit
; nothing
FunctionEnd

Function nsDialogsPage
.getcode:
	nsDialogs::Create /NOUNLOAD 1018
	Pop $Dialog
	
	!insertmacro MUI_HEADER_TEXT $(PAGE_TITLE) $(PAGE_SUBTITLE)

	${If} $Dialog == error
		Abort
	${EndIf}
	
		${NSD_CreateLabel} 0 0 100% 12u "Please enter your install code:"
	Pop $Label

	${NSD_CreateText} 0 20u 75u 11u ""
	Pop $Installcode
	
	nsDialogs::Show
FunctionEnd 

Function nsDialogsPageLeave

; remove this section after all is working.

	${NSD_GetText} $Installcode $R0
	MessageBox MB_OK "You typed:$\n$\n$R0"

FunctionEnd

;---------------------------------
; Sections
  
Section "Set Outpath" SecOutpath

  ; Set output path to the installation directory, since we only need it temporarily anyway.
  SetOutPath $INSTDIR

SectionEnd ; end the section


Section "Get Card" SecGetcard

; This section parses the install code we received in Function nsDialogsPage, which we saved to $R0, 
; and passes it into a URL to download. Install code is really just a directory name.

    SetDetailsView show

    inetc::get /caption "Download dialog" /popup "" "http://www.emailideas.com/eig/ecards/$R0/sig.html" "$INSTDIR\Emailideas-eCard-${MyDATESTAMP}.html" /end
    Pop $0 # return value = exit code, "OK" means a file was retrieved.
	
; Check and see if file was found. If ok, continue to registry edits. If not, go back to .getcode again.  

	StrCmp $0 "OK" continue
	MessageBox MB_RETRYCANCEL "Install code not found, or server unreachable.$\n Retry?" IDRETRY .getcode
  Abort "Installation cancelled."
	
; We're good, continue. 

continue:	
    MessageBox MB_OK "eCard found, download successful."
	
SectionEnd

Section "Registry Modification" SecRegmod

; Write the registry mods changes.

	${registry::Write} "HKEY_CURRENT_USER\Software\A1Test\Windows Mail\signatures\00000000" "file" "$INSTDIR\Emailideas-eCard-${MyDATESTAMP}.html" "REG_SZ" $R0

	${registry::Write} "HKEY_CURRENT_USER\Software\A1Test\Windows Mail\signatures\00000000" "name" "eCard from EmailIdeas - Success" "REG_SZ" $R1
		
	${registry::Write} "HKEY_CURRENT_USER\Software\A1Test\Windows Mail\signatures\00000000" "type" "2" "REG_DWORD" $R2
	
	${registry::Write} "HKEY_CURRENT_USER\Software\A1Test\Windows Mail" "Signature Flags" "3" "REG_DWORD" $R3
	
;consolidated the popups...	 get rid of these when all is working.

	MessageBox MB_OK "registry::Write$\n$\n\
			'file' Errorlevel: [$R0]$\n\
			'name' Errorlevel: [$R1]$\n\
			'type' Errorlevel: [$R2]$\n\
			'Signature Flags' Errorlevel: [$R3]$\n"

SectionEnd



