anchor.diff:          "   <UIRef Id='WixUI_Mondo' />\n"
           "   <UIRef Id='WixUI_ErrorProgressText' />\n\n"),
   
  -    [Item] = dets:select(?TAB, [{#item{type=file, name="dderl.cmd"
  +    %% Service Installation
  +    [Comp] = dets:select(?TAB, [{#item{type=component
  +                                       , name="dderl.cmd", _='_'}
  +                                 , [], ['$_']}]),
  +    [CItm] = dets:select(?TAB, [{#item{type=file, name="dderl.cmd"
                                          , guid=undefined, _='_'}
                                    , [], ['$_']}]),
   
  -    ok = file:write(FileH,
  -        "   <CustomAction Id='InstallService' FileKey='"++Item#item.id++"'"
  -                " ExeCommand='install' Execute='commit' />\n"
  -        "   <CustomAction Id='StartService' FileKey='"++Item#item.id++"'"
  -                " ExeCommand='start' Execute='commit' />\n"),
  -
  +    % Custom actions service install and start
  +    %  must run after InstallFiles step is 'comitted'
  +    % Custom actions service stop and uninstall
  +    %  must run immediately and before InstallValidate
  +    %  step to ensure that installed files are not
  +    %  removed and DDErl service is stopped before
  +    %  uninstalling process detecets and warns
  +    ok = file:write(FileH,
  +        "   <CustomAction Id='InstallService' FileKey='"++CItm#item.id++"'\n"
  +        "                 ExeCommand='install' Execute='commit' />\n"
  +        "   <CustomAction Id='StartService' FileKey='"++CItm#item.id++"'\n"
  +        "                 ExeCommand='start' Execute='commit' />\n"
  +        "   <CustomAction Id='UnInstallService' FileKey='"++CItm#item.id++"'\n"
  +        "                 ExeCommand='uninstall' Execute='immediate' />\n"
  +        "   <CustomAction Id='StopService' FileKey='"++CItm#item.id++"'\n"
  +        "                 ExeCommand='stop' Execute='immediate' />\n\n"
  +                   ),
  +
  +    % Sequence of custom action is important to ensure
  +    %  service is installed before started and stopped
  +    %  before uninstalling
  +    % Also ComponentId = # is used to identify the service
  +    %  controlling script is executed in correct execution path
  +    %  2 - Uninstalling
  +    %  3 - Installing
  +    %  Ref http://wix.tramontana.co.hu/tutorial/com-expression-syntax-miscellanea/expression-syntax
       ok = file:write(FileH,
           "   <InstallExecuteSequence>\n"
  -        "      <Custom Action='InstallService' After='InstallFiles' />\n"
  -        "      <Custom Action='StartService' Before='InstallFiles' />\n"
  -        "   </InstallExecuteSequence>\n\n"
  +        "      <Custom Action='StopService' Before='InstallValidate'>"
  +                "$"++Comp#item.id++"=2</Custom>\n"
  +        "      <Custom Action='UnInstallService' Before='InstallValidate'>"
  +                "$"++Comp#item.id++"=2</Custom>\n"
  +        "      <Custom Action='InstallService' After='InstallFiles'>"
  +                "$"++Comp#item.id++"=3</Custom>\n"
  +        "      <Custom Action='StartService' After='InstallFiles'>"
  +                "$"++Comp#item.id++"=3</Custom>\n"
  +        "   </InstallExecuteSequence>\n\n"),
  +
  +    ok = file:write(FileH,
           "</Product>\n"
           "</Wix>"),
  +
       ok = file:close(FileH),
       {ok, CurDir} = file:get_cwd(),
       ok = file:set_cwd(filename:join([Root,"rel", "wixsetup"])),
  
positive.code:                 component -> {Item#item.id, Item#item.guid};
                  file -> {ok, Item#item.id};
                  dir -> {ok, Item#item.id}
              end
      end;
  get_id(undefined, Field, undefined) when is_list(Field) ->
  
negative.code:         "       </Directory>\n"
          "     </Directory>\n"),
  
      {RegId, RegGuId} = get_id(component, "dderl"
                                , filename:join([Root,"rel"])),
      ok = file:write(FileH,
          "     <Directory Id='ProgramMenuFolder' Name='Programs'>\n"
          "        <Directory Id='"++RegId++"'"
                                  " Name='"?PRODUCT" "?VERSION"'>\n"
          "           <Component Id='"++RegId++"' Guid='"++RegGuId++"'>\n"
          "               <RemoveFolder Id='"++RegId++"' On='uninstall' />\n"
          "               <RegistryValue Root='HKCU'"
                              " Key='Software\\[Manufacturer]\\[ProductName]'"
                              " Type='string' Value='' KeyPath='yes' />\n"
          "           </Component>\n"
          "       </Directory>\n"
          "     </Directory>\n\n"),
  