# Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

!include EnvVarUpdate.nsh

Var Dialog
Var FirstTime

Var Label

Var CheckboxHOME
Var CheckboxHOME_State

Var CheckboxScripts
Var CheckboxScripts_State

Var CheckboxRadioButtons
Var CheckboxRadioButtons_State

Var GroupBoxRadioButton

Var RadioButtonX64_VS2015
Var RadioButtonX64_VS2015_State

Var RadioButtonI86_VS2015
Var RadioButtonI86_VS2015_State

Var RadioButtonX64_VS2017
Var RadioButtonX64_VS2017_State

Var RadioButtonI86_VS2017
Var RadioButtonI86_VS2017_State

LangString PAGE_TITLE ${LANG_ENGLISH} "Environment variables setting"
LangString PAGE_SUBTITLE ${LANG_ENGLISH} "Choose which environment variables you want to set."

Function VariablesEntornoPage

    !insertmacro MUI_HEADER_TEXT $(PAGE_TITLE) $(PAGE_SUBTITLE)

    nsDialogs::Create 1018
    Pop $Dialog

    ${If} $Dialog == error
        Abort
    ${EndIf}

    ### Creacion de los elementos graficos
    ${NSD_CreateLabel} 0 0 100% 20u "Check the environment variables you want to set and uncheck the environment variables you don't want to set. Click Next to continue."
    Pop $Label

    ${NSD_CreateCheckbox} 10 20u 100% 12u "Set the @PROJECT_NAME_UPPER@HOME environment variable."
    Pop $CheckboxHOME
    ${If} $CheckboxHOME_State == ${BST_CHECKED}
        ${NSD_Check} $CheckboxHOME
    ${EndIf}

    ${NSD_CreateCheckbox} 10 32u 100% 12u "&Add to the PATH environment variable the location of @PROJECT_NAME_LARGE@ scripts"
    Pop $CheckboxScripts
    ${If} $CheckboxScripts_State == ${BST_CHECKED}
        ${NSD_Check} $CheckboxScripts
    ${EndIf}

    ${NSD_CreateCheckbox} 10 44u 100% 12u "&Add to the PATH the location of eProsima @PROJECT_NAME_LARGE@ libraries."
    Pop $CheckboxRadioButtons

    ### La primera vez que lanzamos el instalador, el checkbox de HOME
    ### y el de SCRIPTS deben estar marcados. 
    StrCmp $FirstTime "FirstTime" jumpfirsttime 0 ### Si son iguales las cadenas, GOTO +5, si no, GOTO 0
        ${NSD_Check} $CheckboxHOME
        ${NSD_Check} $CheckboxScripts
        ${NSD_Check} $CheckboxRadioButtons
        ${NSD_GetState} $CheckboxHOME $CheckboxHOME_State
        ${NSD_GetState} $CheckboxScripts $CheckboxScripts_State
        ${NSD_GetState} $CheckboxRadioButtons $CheckboxRadioButtons_State
        StrCpy $FirstTime "FirstTime"

    jumpfirsttime:

    Call CheckRadioButtons

    !define lib_radio_x0 30u
    !define lib_radio_x1 140u
    !define lib_radio_height 15u
    !define lib_radio_y_0 71u
    !define lib_radio_y_1 86u
    !define lib_radio_y_2 101u

    ; GroupBox 1     
    ${NSD_CreateGroupBox} 20u 56u 90% 97u "Select the configuration:" 
    Pop $GroupBoxRadioButton   
    #Pop $GB_Ar1 

    ${If} ${RunningX64}
        ${NSD_CreateRadioButton} ${lib_radio_x0} ${lib_radio_y_0} 30% ${lib_radio_height} "&x64 VS2017 platforms."
        Pop $RadioButtonX64_VS2017
        ### Fijamos los callbacks para cuando se haga click en los RadioButtones
        ${NSD_OnClick} $RadioButtonX64_VS2017 ClickX64_VS2017

        ${NSD_CreateRadioButton} ${lib_radio_x0} ${lib_radio_y_1} 30% ${lib_radio_height} "&x64 VS2015 platforms."
        Pop $RadioButtonX64_VS2015
        ### Fijamos los callbacks para cuando se haga click en los RadioButtones
        ${NSD_OnClick} $RadioButtonX64_VS2015 ClickX64_VS2015

        ${NSD_CreateRadioButton} ${lib_radio_x1} ${lib_radio_y_0} 30% ${lib_radio_height} "&i86 VS2017 platforms."
        Pop $RadioButtonI86_VS2017
        ${NSD_CreateRadioButton} ${lib_radio_x1} ${lib_radio_y_1} 30% ${lib_radio_height} "&i86 VS2015 platforms."
        Pop $RadioButtonI86_VS2015

        Call EnableRadioX64
        Call EnableRadioI86	
    ${Else}
        ${NSD_CreateRadioButton} ${lib_radio_x0} ${lib_radio_y_0} 50% ${lib_radio_height} "&i86 VS2017 platforms."
        Pop $RadioButtonI86_VS2017
        ${NSD_CreateRadioButton} ${lib_radio_x0} ${lib_radio_y_1} 50% ${lib_radio_height} "&i86 VS2015 platforms."
        Pop $RadioButtonI86_VS2015

        Call EnableRadioI86	
    ${EndIf}

    ### Fijamos los callbacks para cuando se haga click en los CheckBoxes
    ${NSD_OnClick} $CheckboxHOME ClickHOME 
    ${NSD_OnClick} $CheckboxScripts ClickScripts
    ${NSD_OnClick} $CheckboxRadioButtons ClickCheckboxRadioButtons
    ${NSD_OnClick} $RadioButtonI86_VS2017 ClickI86_VS2017
    ${NSD_OnClick} $RadioButtonI86_VS2015 ClickI86_VS2015

    Call SelectDefaultBestConfiguration

    nsDialogs::Show
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox HOME
### Guardamos el estado en la variable _state
Function ClickHOME
    ${NSD_GetState} $CheckboxHOME $CheckboxHOME_State
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox Scripts
### Guardamos el estado en la variable _state
Function ClickScripts
    ${NSD_GetState} $CheckboxScripts $CheckboxScripts_State
FunctionEnd

Function ClickCheckboxRadioButtons
    ${NSD_GetState} $CheckboxRadioButtons $CheckboxRadioButtons_State
    ${If} $CheckboxRadioButtons_State == 0
        EnableWindow $RadioButtonX64_VS2015 0
        EnableWindow $RadioButtonI86_VS2015 0
        EnableWindow $RadioButtonX64_VS2017 0
        EnableWindow $RadioButtonI86_VS2017 0

        ${NSD_SetState} $RadioButtonX64_VS2015 0
        ${NSD_SetState} $RadioButtonI86_VS2015 0
        ${NSD_SetState} $RadioButtonX64_VS2017 0
        ${NSD_SetState} $RadioButtonI86_VS2017 0

        ${NSD_GetState} $RadioButtonI86_VS2015 $RadioButtonI86_VS2015_State
        ${NSD_GetState} $RadioButtonX64_VS2015 $RadioButtonX64_VS2015_State
        ${NSD_GetState} $RadioButtonX64_VS2017 $RadioButtonX64_VS2017_State
        ${NSD_GetState} $RadioButtonI86_VS2017 $RadioButtonI86_VS2017_State
    ${ElseIf} $CheckboxRadioButtons_State == 1
        Call EnableRadioX64
        Call EnableRadioI86
        Call SelectDefaultBestConfiguration
    ${EndIf}
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox x64 VS2017
### Sirve para deshabilitar el i86 2017, pues no pueden aparecer a la vez
### Tambien guardamos el estado en la variable _state
Function ClickX64_VS2017
    Call ResetStates
    Pop $RadioButtonX64_VS2017
    ${NSD_GetState} $RadioButtonX64_VS2017 $RadioButtonX64_VS2017_State
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox i86
### Sirve para deshabilitar el x64, pues no pueden aparecer a la vez
### Tambien guardamos el estado en la variable _state
Function ClickI86_VS2017
    Call ResetStates
    Pop $RadioButtonI86_VS2017
    ${NSD_GetState} $RadioButtonI86_VS2017 $RadioButtonI86_VS2017_State
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox x64
### Sirve para deshabilitar el i86, pues no pueden aparecer a la vez
### Tambien guardamos el estado en la variable _state
Function ClickX64_VS2015
    Call ResetStates
    Pop $RadioButtonX64_VS2015
    ${NSD_GetState} $RadioButtonX64_VS2015 $RadioButtonX64_VS2015_State
FunctionEnd

### Callback invocado cuando se pulsa el CheckBox i86
### Sirve para deshabilitar el x64, pues no pueden aparecer a la vez
### Tambien guardamos el estado en la variable _state
Function ClickI86_VS2015
    Call ResetStates
    Pop $RadioButtonI86_VS2015
    ${NSD_GetState} $RadioButtonI86_VS2015 $RadioButtonI86_VS2015_State
FunctionEnd

Function ResetStates
    Push  0
    Pop $RadioButtonX64_VS2015_State
    Push  0
    Pop $RadioButtonI86_VS2015_State
    Push  0
    Pop $RadioButtonX64_VS2017_State
    Push  0
    Pop $RadioButtonI86_VS2017_State
FunctionEnd

Function SelectDefaultBestConfiguration
    ${If} $CheckboxRadioButtons_State == 1
        ${If} ${SectionIsSelected} ${libraries_x64Win64VS2017}
            ${NSD_SetState} $RadioButtonX64_VS2017 1
            ${NSD_GetState} $RadioButtonX64_VS2017 $RadioButtonX64_VS2017_State
        ${ElseIf} ${SectionIsSelected} ${libraries_x64Win64VS2015}
            ${NSD_SetState} $RadioButtonX64_VS2015 1
            ${NSD_GetState} $RadioButtonX64_VS2015 $RadioButtonX64_VS2015_State
        ${ElseIf} ${SectionIsSelected} ${libraries_i86Win32VS2017}
            ${NSD_SetState} $RadioButtonI86_VS2017 1
            ${NSD_GetState} $RadioButtonI86_VS2017 $RadioButtonI86_VS2017_State
        ${ElseIf} ${SectionIsSelected} ${libraries_i86Win32VS2015}
            ${NSD_SetState} $RadioButtonI86_VS2015 1
            ${NSD_GetState} $RadioButtonI86_VS2015 $RadioButtonI86_VS2015_State		
        ${EndIf}
    ${EndIf}
FunctionEnd

Function CheckRadioButtons
   ${If} ${SectionIsSelected} ${libraries}
   ${OrIf} ${SectionIsPartiallySelected} ${libraries}
        EnableWindow $CheckboxRadioButtons 1
        ${If} $CheckboxRadioButtons_State == 1
            ${NSD_Check} $CheckboxRadioButtons
        ${EndIf}
    ${Else}
        EnableWindow $CheckboxRadioButtons 0
        ${NSD_SetState} $CheckboxRadioButtons 0
    ${EndIf}

FunctionEnd

Function EnableRadioX64
    ${If} ${RunningX64}
        ${If} $CheckboxRadioButtons_State == 1
            ${If} ${SectionIsSelected} ${libraries_x64Win64VS2017}
                EnableWindow $RadioButtonX64_VS2017 1
            ${Else}
                EnableWindow $RadioButtonX64_VS2017 0
            ${EndIf}
            ${If} ${SectionIsSelected} ${libraries_x64Win64VS2015}
                EnableWindow $RadioButtonX64_VS2015 1
            ${Else}
                EnableWindow $RadioButtonX64_VS2015 0
            ${EndIf}
        ${Else}
            EnableWindow $RadioButtonX64_VS2017 0
            EnableWindow $RadioButtonX64_VS2015 0
        ${EndIf}
    ${EndIf}
FunctionEnd

Function EnableRadioI86
    ${If} $CheckboxRadioButtons_State == 1
        ${If} ${SectionIsSelected} ${libraries_i86Win32VS2017}
            EnableWindow $RadioButtonI86_VS2017 1
        ${Else}
            EnableWindow $RadioButtonI86_VS2017 0
        ${EndIf}
        ${If} ${SectionIsSelected} ${libraries_i86Win32VS2015}
            EnableWindow $RadioButtonI86_VS2015 1
        ${Else}
            EnableWindow $RadioButtonI86_VS2015 0
        ${EndIf}
    ${Else}
        EnableWindow $RadioButtonI86_VS2017 0
        EnableWindow $RadioButtonI86_VS2015 0
    ${EndIf}
FunctionEnd

Function InstallVarEnvs
    ### Actualizamos las variables de entorno que se hayan marcado
    ${If} $CheckboxHOME_State == ${BST_CHECKED}
       ${EnvVarUpdate} $0 "@PROJECT_NAME_UPPER@HOME" "P" "HKLM" "$INSTDIR"
       WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "@PROJECT_NAME_UPPER@HOME" 1
    ${EndIf}
    ${If} $CheckboxScripts_State == ${BST_CHECKED}
       ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR\bin"
       WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "Script location" 1
    ${EndIf}
    ${If} ${SectionIsSelected} ${libraries_x64Win64VS2017}
        ${If} $RadioButtonX64_VS2017_State == ${BST_CHECKED}
             ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR\bin\x64Win64VS2017"
             WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2017 libraries location" 1
        ${EndIf}
    ${EndIf}
    ${If} ${SectionIsSelected} ${libraries_i86Win32VS2017}
        ${If} $RadioButtonI86_VS2017_State == ${BST_CHECKED}
             ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR\bin\i86Win32VS2017"
             WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2017 libraries location" 1
        ${EndIf}
    ${EndIf}
    ${If} ${SectionIsSelected} ${libraries_x64Win64VS2015}
        ${If} $RadioButtonX64_VS2015_State == ${BST_CHECKED}
             ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR\bin\x64Win64VS2015"
             WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2015 libraries location" 1
        ${EndIf}
    ${EndIf}
    ${If} ${SectionIsSelected} ${libraries_i86Win32VS2015}
        ${If} $RadioButtonI86_VS2015_State == ${BST_CHECKED}
             ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR\bin\i86Win32VS2015"
             WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2015 libraries location" 1
        ${EndIf}
    ${EndIf}
FunctionEnd

Var RegKeyCheck

!macro UninstallVarEnvs
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "@PROJECT_NAME_UPPER@HOME"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "@PROJECT_NAME_UPPER@HOME"
    ${un.EnvVarUpdate} $0 "@PROJECT_NAME_UPPER@HOME" "R" "HKLM" "$INSTDIR"
  ${EndIf}
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "Script location"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "Script location"
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR\bin"
  ${EndIf}
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2017 libraries location"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2017 libraries location"
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR\bin\x64Win64VS2017"
  ${EndIf}
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2017 libraries location"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2017 libraries location"
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR\bin\i86Win32VS2017"
  ${EndIf}
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2015 libraries location"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "x64Win64VS2015 libraries location"
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR\bin\x64Win64VS2015"
  ${EndIf}
  ReadRegStr $RegKeyCheck HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2015 libraries location"
  ${If} $RegKeyCheck == 1
    DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "i86Win32VS2015 libraries location"
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR\bin\i86Win32VS2015"
  ${EndIf}
!macroend
