1/*
2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "resource.h"
30
31#include <shlwapi.h>
32#include <stdio.h>
33#include <tchar.h>
34#include <windows.h>
35
36#define LOG(header, ...) \
37    do { \
38        _ftprintf(stderr, header); \
39        _ftprintf(stderr, __VA_ARGS__); \
40    } while (0)
41#define LOG_WARNING(...) LOG(TEXT("WARNING: "), __VA_ARGS__)
42#define LOG_ERROR(...) LOG(TEXT("ERROR: "), __VA_ARGS__)
43
44static TCHAR* getStringValue(HKEY key, LPCTSTR valueName)
45{
46    DWORD type = 0;
47    DWORD bufferSize = 0;
48    if (RegQueryValueEx(key, valueName, 0, &type, 0, &bufferSize) != ERROR_SUCCESS || type != REG_SZ)
49        return 0;
50
51    TCHAR* buffer = (TCHAR*)malloc(bufferSize);
52    if (RegQueryValueEx(key, valueName, 0, &type, reinterpret_cast<LPBYTE>(buffer), &bufferSize) != ERROR_SUCCESS) {
53        free(buffer);
54        return 0;
55    }
56
57    return buffer;
58}
59
60static TCHAR* getInstalledWebKitDirectory()
61{
62    LPCTSTR installPathKeyString = TEXT("SOFTWARE\\Apple Computer, Inc.\\Safari");
63    LPCTSTR installPathWin64KeyString = TEXT("SOFTWARE\\Wow6432Node\\Apple Computer, Inc.\\Safari");
64    HKEY installPathKey = 0;
65    LONG error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, installPathKeyString, 0, KEY_READ, &installPathKey);
66    if (error != ERROR_SUCCESS)
67        error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, installPathWin64KeyString, 0, KEY_READ, &installPathKey);
68    if (error != ERROR_SUCCESS) {
69        LOG_WARNING(TEXT("Failed to open registry key %s\n"), installPathKeyString);
70        return 0;
71    }
72    LPTSTR webKitPath = getStringValue(installPathKey, TEXT("InstallDir"));
73    RegCloseKey(installPathKey);
74    if (!webKitPath) {
75        LOG_WARNING(TEXT("Couldn't retrieve value for registry key %s\n"), installPathKeyString);
76        return 0;
77    }
78    return webKitPath;
79}
80
81int _tmain(int argc, TCHAR* argv[])
82{
83    TCHAR* path = getInstalledWebKitDirectory();
84    if (!path) {
85        LOG_ERROR(TEXT("Couldn't determine installed Safari path\n"));
86        return 1;
87    }
88
89    bool printLauncher = false;
90    bool printEnvironment = false;
91    bool debugger = false;
92
93    for (int i = 1; i < argc; ++i) {
94        if (!_tcscmp(argv[i], TEXT("/printSafariLauncher"))) {
95            printLauncher = true;
96            continue;
97        }
98        if (!_tcscmp(argv[i], TEXT("/printSafariEnvironment"))) {
99            printEnvironment = true;
100            continue;
101        }
102        if (!_tcscmp(argv[i], TEXT("/debugger"))) {
103            debugger = true;
104            continue;
105        }
106    }
107
108    // printLauncher is inclusive of printEnvironment, so do not
109    // leave both enabled:
110    if (printLauncher && printEnvironment)
111        printEnvironment = false;
112
113    if (!printLauncher && !printEnvironment) {
114        _tprintf(TEXT("%s\n"), path);
115        free(path);
116        return 0;
117    }
118
119    LPCTSTR lines[] = {
120        TEXT("@echo off"),
121        TEXT("del /s /q \"%%TMP%%\\WebKitNightly\""),
122        TEXT("mkdir 2>NUL \"%%TMP%%\\WebKitNightly\\Safari.resources\""),
123        TEXT("mkdir 2>NUL \"%%TMP%%\\WebKitNightly\\WebKit.resources\""),
124        TEXT("mkdir 2>NUL \"%%TMP%%\\WebKitNightly\\JavaScriptCore.resources\""),
125        TEXT("xcopy /y /i /d \"%sSafari.exe\" \"%%TMP%%\\WebKitNightly\""),
126        TEXT("if exist \"%sSafari.dll\" xcopy /y /i /d \"%sSafari.dll\" \"%%TMP%%\\WebKitNightly\""),
127        TEXT("xcopy /y /i /d /e \"%sSafari.resources\" \"%%TMP%%\\WebKitNightly\\Safari.resources\""),
128        TEXT("xcopy /y /i /d /e \"%splugins\" \"%%TMP%%\\WebKitNightly\\plugins\""),
129        TEXT("xcopy /y /i /d WebKit.dll \"%%TMP%%\\WebKitNightly\""),
130        TEXT("xcopy /y /i /d WebKit.pdb \"%%TMP%%\\WebKitNightly\""),
131        TEXT("xcopy /y /i /d /e WebKit.resources \"%%TMP%%\\WebKitNightly\\WebKit.resources\""),
132        TEXT("xcopy /y /i /d JavaScriptCore.dll \"%%TMP%%\\WebKitNightly\""),
133        TEXT("xcopy /y /i /d JavaScriptCore.pdb \"%%TMP%%\\WebKitNightly\""),
134        TEXT("xcopy /y /i /d /e JavaScriptCore.resources \"%%TMP%%\\WebKitNightly\\JavaScriptCore.resources\""),
135        TEXT("set PATH=%%CD%%;%s;%%PATH%%"),
136    };
137
138    LPCTSTR command = TEXT("\"%TMP%\\WebKitNightly\\Safari.exe\"");
139
140    LPCTSTR launchLines[] = {
141        TEXT("%s"),
142    };
143
144    LPCTSTR debuggerLines[] = {
145        TEXT("if exist \"%%DevEnvDir%%\\VCExpress.exe\" ("),
146        TEXT("\"%%DevEnvDir%%\\VCExpress.exe\" /debugExe %s"),
147        TEXT(") else ("),
148        TEXT("\"%%DevEnvDir%%\\devenv.exe\" /debugExe %s"),
149        TEXT(")"),
150    };
151
152    for (int i = 0; i < ARRAYSIZE(lines); ++i) {
153        _tprintf(lines[i], path, path);
154        _tprintf(TEXT("\n"));
155    }
156
157    LPCTSTR* endLines = debugger ? debuggerLines : launchLines;
158
159    // Don't print launch command if we just want the environment set up...
160    if (!printEnvironment) {
161       for (unsigned i = 0; i < (debugger ? ARRAYSIZE(debuggerLines) : ARRAYSIZE(launchLines)); ++i) {
162           _tprintf(endLines[i], command);
163           _tprintf(TEXT("\n"));
164       }
165    }
166
167    free(path);
168    return 0;
169}
170