Index: wisotool
===================================================================
--- wisotool	(révision 1206)
+++ wisotool	(copie de travail)
@@ -340,7 +340,7 @@
 }
 
 # Download a file
-# Usage: package url [sha1sum [filename]]
+# Usage: package url [sha1sum [filename [cookie jar]]]
 # Caches downloads in wisotoolcache/$package
 download() {
     if [ "$4"x != ""x ]
@@ -366,11 +366,11 @@
            # [*] --read-timeout is useful on the adobe server that doesn't
            # close the connection unless you tell it to (control-C or closing
            # the socket)
-           try wget -O "$file" -nd -c --read-timeout=300 --retry-connrefused --header "Accept-Encoding: gzip,deflate" "$2"
+           try wget -O "$file" -nd -c --read-timeout=300 --retry-connrefused --header "Accept-Encoding: gzip,deflate" ${5:+--load-cookies "$5"} "$2"
         else
            # curl doesn't get filename from the location given by the server!
            # fortunately, we know it
-           try curl -L -o "$file" -C - --header "Accept-Encoding: gzip,deflate" "$2"
+           try curl -L -o "$file" -C - --header "Accept-Encoding: gzip,deflate" ${5:+--cookie "$5"} "$2"
         fi
         # Need to decompress .exe's that are compressed, else cygwin fails
         # Only affects cygwin, so don't barf if 'file' not installed
@@ -2177,6 +2177,186 @@
 
 #----------------------------------------------------------------
 
+# Generic GOG.com installer
+# Usage: game_id game_title [other_files [reader_control [run_command [download_id]]]]
+# game_id
+#     Used for main installer name and download url.
+# game_title
+#     Used for AutoHotKey and installation path in bat script.
+# other_files
+#     Extra installer files, in one string, space-separated.
+# reader_control
+#     If set, the control id of the configuration pannel checkbox controling
+#     Adobe Reader installation.
+#     Some games don't have it, some games do with different ids.
+# run_command
+#     Used for bat script, relative to installation path.
+# download_id
+#     For games which download url doesn't match their game_id
+_load_gog()
+{
+    # TODO: support SHA1 checks ? (installer verifies files)
+    load_autohotkey
+
+    game_id="$1"
+    game_title="$2"
+    other_files="$3"
+    reader_control="$4"
+    run_command="$5"
+    download_id="$6"
+
+    if [ "$download_id"x == ""x ]
+    then
+        download_id="$game_id"
+    fi
+
+    installer_path="$WISOTOOL_CACHE/gog"
+    mkdir -p "$installer_path"
+    auth_path="$WISOTOOL_CACHE/gog_auth"
+    mkdir -p "$auth_path"
+    installer="setup_$game_id.exe"
+    cookie="$auth_path/cookie.txt"
+
+    file_id=0
+    for file in $installer $other_files
+    do
+        file_path="$installer_path/$file"
+        if [ ! -s "$file_path" ]
+        then
+            if [ ! -f "$cookie" ]
+            then
+                login_file="$auth_path/login.txt"
+                password_file="$auth_path/password.txt"
+                if [ ! -f "$login_file" -o ! -f "$password_file" ]
+                then
+                    die "Please put your GOG.com login in '$login_file' and password in '$password_file' or put '$file' in '$installer_path'."
+                fi
+                base_url="https://www.gog.com"
+                login_url="/en/login/ajax/"
+                next_url="$WISOTOOL_TMP/gog_redir_url"
+                post_data="$WISOTOOL_TMP/gog_post_data"
+                # Note: just write login & password to a file so they don't appear in "ps"
+                echo "a=check&t=old&u=`cat \"$login_file\" | sed 's/&/%26/g'`&p=`cat \"$password_file\" | sed 's/&/%26/g'`&r=frontpage" > "$post_data"
+                if [ -x "`which wget 2>/dev/null`" ]
+                then
+                    try wget -O "$next_url" --save-cookies "$cookie" --post-file "$post_data" "$base_url$login_url"
+                    try wget -O /dev/null --load-cookies "$cookie" --save-cookies "$cookie" "$base_url`cat \"$next_url\"`"
+                else
+                    try curl -o "$next_url" --cookie-jar "$cookie" --data "@$post_data" "$base_url$login_url"
+                    try curl -o /dev/null --cookie "$cookie" --cookie-jar "$cookie" "$base_url`cat \"$next_url\"`"
+                fi
+                rm -f "$post_data"
+                rm -f "$next_url"
+            fi
+            download "gog" "https://www.gog.com/en/download/game/$download_id/$file_id" "" "$file" "$cookie"
+        fi
+        file_id=$(( $file_id + 1 ))
+    done
+
+    cd "$installer_path"
+    ahk_do "
+        run $installer
+        WinWait, Setup - $game_title, Start installation
+        ControlGet, checkbox_state, Checked,, TCheckBox1 ; EULA
+        if (checkbox_state != 1) {
+            ControlClick, TCheckBox1
+        }
+        if (\"$reader_control\") {
+            ControlClick, TMCoPShadowButton1 ; Options
+            Loop, 10
+            {
+                ControlGet, visible, Visible,, $reader_control
+                if (visible)
+                {
+                    break
+                }
+                Sleep, 1000
+            }
+            ControlGet, checkbox_state, Checked,, $reader_control ; Unckeck Adobe/Foxit Reader
+            if (checkbox_state != 0) {
+                ControlClick, $reader_control
+            }
+        }
+        ControlClick, TMCoPShadowButton2 ; Start Installation
+        WinWait, Setup - $game_title, Exit Installer
+        ControlClick, TMCoPShadowButton1 ; Exit Installer
+        "
+
+    if [ "$run_command"x != ""x ]
+    then
+        cat > "$DRIVE_C/run-$PACKAGE.bat" <<__EOF__
+cd "$programfilesdir_x86_win\GOG.com\\$game_title"
+$run_command
+__EOF__
+    fi
+}
+
+verblist_add dukenukem3d_gog "Duke Nukem 3D (GOG.com)"
+
+load_dukenukem3d_gog()
+{
+    _load_gog "duke3d" "Duke Nukem 3D" "" "TsCheckBox2" "DOSBOX\dosbox.exe -conf dosboxDuke3D.conf -noconsole -c \"exit\"" "duke_nukem_3d_atomic_edition"
+}
+
+verblist_add dukenukemmp_gog "Duke Nukem Manhattan Project (GOG.com)"
+
+load_dukenukemmp_gog()
+{
+    _load_gog "duke_nukem_manhattan_project" "Duke Nukem - Manhattan Project" "" "TsCheckBox2" "DukeNukemMP.exe"
+}
+
+verblist_add ut2004_gog "Unreal Tournament 2004 (GOG.com)"
+
+load_ut2004_gog()
+{
+    _load_gog "ut2004" "Unreal Tournament 2004" "setup_ut2004-1.bin setup_ut2004-2.bin" "TsCheckBox2" "system\ut2004.exe"
+}
+
+verblist_add abes_oddysee_gog "Abe's Oddysee (GOG.com)"
+
+load_abes_oddysee_gog()
+{
+    _load_gog "abes_oddysee" "Abe's Oddysee" "" "TsCheckBox2" "AbeWin.exe"
+}
+
+verblist_add abes_exoddus_gog "Abe's Exoddus (GOG.com)"
+
+load_abes_exoddus_gog()
+{
+    _load_gog "abes_exoddus" "Abe's Exoddus" "" "TsCheckBox2" "Exoddus.exe"
+}
+
+verblist_add beyond_good_and_evil_gog "Beyond Good and Evil (GOG.com)"
+
+load_beyond_good_and_evil_gog()
+{
+    _load_gog "beyond_good_and_evil" "Beyond Good and Evil" "" "TsCheckBox2" "gogwrap.exe GOGBEYONDGOODANDEVIL"
+}
+
+verblist_add far_cry_gog "Far Cry (GOG.com)"
+
+load_far_cry_gog()
+{
+    _load_gog "far_cry" "Far Cry" "setup_far_cry-1.bin setup_far_cry-2.bin" "TsCheckBox2" "Bin32\FarCry.exe"
+}
+
+verblist_add descent_1_2_gog "Descent and Descent 2 (GOG.com)"
+
+load_descent_1_2_gog()
+{
+    # XXX: this installs 2 games, how should bat script be ?
+    _load_gog "descent_1_2" "Descent and Descent 2" "" "TsCheckBox5" ""
+}
+
+verblist_add evil_genius_gog "Evil Genius (GOG.com)"
+
+load_evil_genius_gog()
+{
+    _load_gog "evil_genius" "Evil Genius" "" "TsCheckBox2" "ReleaseExe\EvilGeniusExeStub-Release.exe"
+}
+
+#----------------------------------------------------------------
+
 print_version() {
     echo "$VERSION"
 }
