From 532140f5fcec848327d54b471a9ae1505f3e91b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= <georges.duperon@gmail.com>
Date: Fri, 20 Jul 2018 23:44:59 +0200
Subject: [PATCH] Use only mktemp -d, as it is near-impossible to get mktemp to
 create a file with a suffix (extension) on macos

---
 .gitignore                                |  4 +++
 Makefile                                  | 26 +++++++++++++++++++-
 test/bochs.sh                             | 10 +++-----
 test/virtualbox.sh                        |  8 +++---
 utils/ansi-screenshots/ansi_screenshot.sh |  7 +-----
 utils/ansi-screenshots/to_ansi.sh         | 30 +++++------------------
 utils/auto-push.sh                        | 21 ++++++++--------
 utils/gui-wrapper-mac.sh                  | 28 +++++++++++++++++++++
 utils/gui-wrapper.sh                      | 10 +++-----
 utils/make-travis-key.sh                  |  2 +-
 utils/mktemp.sh                           | 10 +++-----
 utils/travis_known_hosts                  |  1 +
 12 files changed, 89 insertions(+), 68 deletions(-)
 create mode 100755 utils/gui-wrapper-mac.sh
 create mode 100644 utils/travis_known_hosts

diff --git a/.gitignore b/.gitignore
index 43a1abf..db39399 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+/build/bochscontinue
+/build/bochsrc
 /build/check_makefile
 /build/check_makefile_targets
 /build/check_makefile_w_arnings
@@ -95,6 +97,8 @@
 /build/test_pass/noemu_zip
 /build/test_pass/sudo_fat12_mount
 /build/test_pass/sudo_iso_mount
+/build/twm_cfg
+/build/virtualbox.img
 /deploy-screenshots/bochs-anim.gif
 /deploy-screenshots/bochs.png
 /deploy-screenshots/dosbox-anim.gif
diff --git a/Makefile b/Makefile
index bc6f493..2837cfd 100644
--- a/Makefile
+++ b/Makefile
@@ -71,6 +71,10 @@ built_files = ${os_filename} \
               build/os.hex_with_offsets \
               build/iso_files/os.zip \
               build/iso_files/boot/iso_boot.sys \
+              build/bochsrc \
+              build/bochscontinue \
+              build/twm_cfg \
+              build/virtualbox.img \
               ${more_offset_dec} \
               ${more_offset_hex} \
               ${tests_emu:test/%=build/test_pass/emu_%} \
@@ -348,6 +352,7 @@ ${tests_emu}: build/test_pass/emu_$$(@F)
 
 build/test_pass/emu_% deploy-screenshots/%.png deploy-screenshots/%-anim.gif: \
  ${os_filename} \
+ build/checkerboard_800x600.xbm \
  utils/gui-wrapper.sh utils/ansi-screenshots/ansi_screenshot.sh utils/ansi-screenshots/to_ansi.sh \
  test/%.sh \
  build/check_makefile \
@@ -405,10 +410,29 @@ test/macos-sh-x11:
 	screencapture deploy-screenshots/macos-sh-x11-screencapture.png
 
 .PHONY: test/macos-sh
-test/macos-sh: build/check_makefile | deploy-screenshots
+test/macos-sh: build/check_makefile \
+               build/checkerboard_1024x768.png \
+               | deploy-screenshots
 	osascript -e 'tell app "Terminal" to activate'
 	osascript -e 'tell app "Terminal" to do script "'"$$PWD"'/os.bat"'
 	sleep 5
 	screencapture deploy-screenshots/screencapture-os-bat.png
 
 # See https://wiki.osdev.org/EFI#Emulation to emulate an UEFI system with qemu, to test the EFI boot from hdd / cd / fd (?).
+
+# Create checkerboard background
+build/checkerboard_%.png: build/check_makefile
+	convert -size "$*" \
+	        tile:pattern:checkerboard \
+	        -auto-level +level-colors 'gray(192),gray(128)' \
+	        $@
+
+build/checkerboard_%.xbm: build/check_makefile
+	convert -size "$*" \
+	        tile:pattern:checkerboard \
+	        -auto-level \
+	        $@
+
+# Temporary files
+build/bochsrc build/bochscontinue build/twm_cfg build/virtualbox.img:
+	touch $@
diff --git a/test/bochs.sh b/test/bochs.sh
index 8b32152..9ddebcd 100755
--- a/test/bochs.sh
+++ b/test/bochs.sh
@@ -6,17 +6,15 @@ if test $# -ne 1 || test "$1" = '-h' -o "$1" = '--help'; then
 fi
 os_filename="$1"
 
-bochsrc="$(./utils/mktemp.sh)"
-cat > "$bochsrc" <<EOF
+cat > "build/bochsrc" <<EOF
 floppya: 1_44=${os_filename}, status=inserted
 boot: floppy
 display_library: sdl
 EOF
 
-bochscontinue="$(./utils/mktemp.sh)"
-echo "continue" > "$bochscontinue"
+echo "continue" > "build/bochscontinue"
 
-bochs -qf "$bochsrc" < "$bochscontinue" &
+bochs -qf "build/bochsrc" < "build/bochscontinue" &
 pid=$!
 runsikulix -r test/check-gradient.sikuli && exitcode=$? || exitcode=$?
 
@@ -24,6 +22,4 @@ runsikulix -r test/check-gradient.sikuli && exitcode=$? || exitcode=$?
 
 kill $pid
 
-rm "${bochsrc}" "${bochscontinue}"
-
 exit $exitcode
diff --git a/test/virtualbox.sh b/test/virtualbox.sh
index ff1dde4..28913f1 100755
--- a/test/virtualbox.sh
+++ b/test/virtualbox.sh
@@ -6,17 +6,16 @@ if test $# -ne 1 || test "$1" = '-h' -o "$1" = '--help'; then
 fi
 os_filename="$1"
 
-img_file="$(./utils/mktemp.sh .img)"
-vbox_dir="$(./utils/mktemp.sh -d .vbox)"
+vbox_dir="$(./utils/mktemp.sh -d)"
 vmname="automatic-os-test-$(date +%s)-$$"
 
-ln -sf "$(readlink -f "$os_filename")" "$img_file"
+ln -sf "$(readlink -f "$os_filename")" "build/virtualbox.img"
 VBoxManage createvm --name "$vmname" --register --basefolder "$vbox_dir"
 VBoxManage modifyvm "$vmname" --hwvirtex off
 VBoxManage modifyvm "$vmname" --nestedpaging off
 VBoxManage modifyvm "$vmname" --pae off
 VBoxManage storagectl "$vmname" --name 'floppy disk drive' --add floppy --bootable on
-VBoxManage storageattach "$vmname" --storagectl 'floppy disk drive' --port 0 --device 0 --type fdd --medium "$img_file"
+VBoxManage storageattach "$vmname" --storagectl 'floppy disk drive' --port 0 --device 0 --type fdd --medium "build/virtualbox.img"
 VBoxManage modifyvm "$vmname" --boot1 floppy
 VBoxManage startvm "$vmname" --type sdl &
 pid=$!
@@ -35,7 +34,6 @@ for i in `seq 10`; do
 done
 
 # Cleanup: remove temporary files and directories.
-rm "$img_file"
 rm "/tmp/$vbox_dir" -fr
 
 exit $exitcode
diff --git a/utils/ansi-screenshots/ansi_screenshot.sh b/utils/ansi-screenshots/ansi_screenshot.sh
index 5088e7b..33cafdd 100755
--- a/utils/ansi-screenshots/ansi_screenshot.sh
+++ b/utils/ansi-screenshots/ansi_screenshot.sh
@@ -2,9 +2,4 @@
 
 set -e
 
-screenshot="$("$(dirname "$0")/../mktemp.sh" .png)"
-
-scrot "$screenshot"
-"$(dirname "$0")/to_ansi.sh" "$screenshot" 128
-
-rm "$screenshot"
+import -window root png:- | "$(dirname "$0")/to_ansi.sh" png:- 128
diff --git a/utils/ansi-screenshots/to_ansi.sh b/utils/ansi-screenshots/to_ansi.sh
index 72ac56c..69c3ace 100755
--- a/utils/ansi-screenshots/to_ansi.sh
+++ b/utils/ansi-screenshots/to_ansi.sh
@@ -5,15 +5,6 @@ set -e
 file="$1"
 width="$2"
 
-mini_png="$("$(dirname "$0")/../mktemp.sh" .png)"
-colors_gif="$("$(dirname "$0")/../mktemp.sh" .gif)"
-indexed_gif="$("$(dirname "$0")/../mktemp.sh" .gif)"
-indexed_pgm="$("$(dirname "$0")/../mktemp.sh" .pgm)"
-odd_lines="$("$(dirname "$0")/../mktemp.sh" .odd)"
-even_lines="$("$(dirname "$0")/../mktemp.sh" .even)"
-odd_lines_px="$("$(dirname "$0")/../mktemp.sh" .odd.px)"
-even_lines_px="$("$(dirname "$0")/../mktemp.sh" .even.px)"
-
 colors=(78,78,78 255,108,96 0,170,0 288,288,182 150,203,254 255,115,253 85,255,255 238,238,238 124,124,124 255,155,147 177,253,121 255,255,145 181,220,254 255,156,254 85,255,255 255,255,255)
 args=()
 for i in `seq 0 15`; do
@@ -25,24 +16,17 @@ for i in `seq 0 15`; do
   args+=(-fuzz 0% -fill "gray(${color})" -opaque "rgb(${colors[$i]})")
 done
 
-   convert "$file" -resize "${width}x" "${mini_png}" \
-&& convert +dither -remap "$(dirname "$0")/travis-palette.gif" "${mini_png}" "${colors_gif}" \
-&& convert "${colors_gif}" "${args[@]}" "${indexed_gif}" \
-&& convert "${indexed_gif}" "${indexed_pgm}" \
-&& tail -n +4 "${indexed_pgm}" \
+   convert "$file" -resize "${width}x" png:- \
+ | convert +dither -remap "$(dirname "$0")/travis-palette.gif" png:- gif:- \
+ | convert gif:- "${args[@]}" gif:- \
+ | convert gif:- pgm:- \
+ | tail -n +4 \
  | hexdump -Cv \
  | sed -n -e 's/^[0-9a-f]*  \(\([0-9a-f]\{2\}  \?\)\{1,16\}\).*$/\1/p' \
  | tr '\n' ' ' \
  | sed -e 's/  \+/ /g' \
  | fold -w $((width*3)) \
- | awk "{ if (NR % 2 == 0) print > \"$even_lines\"; else print > \"$odd_lines\"; }"
-nodd="$($(dirname "$0")/../file-length.sh -l "$odd_lines")"
-neven="$($(dirname "$0")/../file-length.sh -l "$even_lines")"
-if test "$nodd" -gt "$neven"; then nlines="$neven"; else nlines="$nodd"; fi
-head -n "$nlines" "$odd_lines"  | sed -e 's/ $//' | tr ' ' '\n' > "$odd_lines_px"
-head -n "$nlines" "$even_lines" | sed -e 's/ $//' | tr ' ' '\n' > "$even_lines_px"
-paste "$odd_lines_px" "$even_lines_px" \
- | tr '\t\n' '  ' \
+ | while read a && read b; do for i in $a; do echo -n "$i ${b%% *} "; b="${b#* }"; done; echo; done \
  | fold -w $((width*6)) \
  | sed -e 's/\([01]\)\([0-7]\) \([01]\)\([0-7]\) /[\1;3\2;4\4mâ–€/g' -e 's/$//' \
  | if test "$CI" = "true" -a "$TRAVIS" = "true"; then sed -e 's/â–€/"/g'; else cat; fi
@@ -52,5 +36,3 @@ paste "$odd_lines_px" "$even_lines_px" \
 # | if test "$CI" = "true" -a "$TRAVIS" = "true"; then while IFS=$'\n' read -n 11 ab; do if test "${#ab}" -ne 11; then echo "$ab"; else echo -n "$ab"; fi; sleep 0.01; done; else cat; fi
 echo
 
-rm "${mini_png}" "${colors_gif}" "${indexed_gif}" "${indexed_pgm}" "${odd_lines}" "${even_lines}" "${odd_lines_px}" "${even_lines_px}"
-
diff --git a/utils/auto-push.sh b/utils/auto-push.sh
index f7ee094..01ea1fa 100755
--- a/utils/auto-push.sh
+++ b/utils/auto-push.sh
@@ -59,26 +59,25 @@ else
     echo "Error while decrypting key."
     exit 1
   fi
+  # TODO: all the config should be in a separate folder, instead of using ~/.ssh for the id_rsa.
   mv travis-deploy-key-id_rsa ~/.ssh/travis-deploy-key-id_rsa
   chmod 600 ~/.ssh/travis-deploy-key-id_rsa
   eval `ssh-agent -s`
   ssh-add ~/.ssh/travis-deploy-key-id_rsa
-  # TODO: all the config should be in a separate folder, instead of using ~/.ssh for the id_rsa.
-  
-  known_hosts_d="$(UMASK=077 "$(dirname "$0")/mktemp.sh" -d)"
-  touch ${known_hosts_d}/known_hosts
-  chmod 600 ${known_hosts_d}/known_hosts
-  echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" >> "${known_hosts_d}/known_hosts"
-  
+
+  travis_known_hosts="$(realpath "$(dirname "$0")/travis_known_hosts")"
+  chmod 600 "$travis_known_hosts"
+  echo "$travis_known_hosts"
+  cat "$travis_known_hosts"
+  ssh -o UserKnownHostsFile=$travis_known_hosts git@github.com || true
+
   TRAVIS_AUTO_PUSH_REPO_DIR="$HOME/travis-temp-auto-push-$(date +%s)"
   if test -e "$TRAVIS_AUTO_PUSH_REPO_DIR"; then rm -rf "$TRAVIS_AUTO_PUSH_REPO_DIR"; fi
-  GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=${known_hosts_d}/known_hosts" git clone -b "$deploy_base_commit" --depth 1 --shallow-submodules "$deploy_repo" "$TRAVIS_AUTO_PUSH_REPO_DIR"
+  GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=$travis_known_hosts" git clone -b "$deploy_base_commit" --depth 1 --shallow-submodules "$deploy_repo" "$TRAVIS_AUTO_PUSH_REPO_DIR"
   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git checkout -b "$deploy_branch")
   rsync -a "${deploy_directory}/" "${TRAVIS_AUTO_PUSH_REPO_DIR}/"
   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git add -A . && git commit --allow-empty -m "Auto-publish to $deploy_branch") > commit.log || (cat commit.log && exit 1)
   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git log --oneline --decorate --graph -10)
   echo '(cd '"$TRAVIS_AUTO_PUSH_REPO_DIR"' && git push --force --quiet "'"$deploy_repo"'" "'"$deploy_branch"'")'
-  (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=${known_hosts_d}/known_hosts" git push --force --quiet "$deploy_repo" "$deploy_branch" >/dev/null 2>&1) >/dev/null 2>&1 # redirect to /dev/null to avoid showing credentials.
-  # Cleanup:
-  rm "${known_hosts_d}/known_hosts"
+  (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=$travis_known_hosts" git push --force --quiet "$deploy_repo" "$deploy_branch" >/dev/null 2>&1) >/dev/null 2>&1 # redirect to /dev/null to avoid showing credentials.
 fi
diff --git a/utils/gui-wrapper-mac.sh b/utils/gui-wrapper-mac.sh
new file mode 100755
index 0000000..c6579fd
--- /dev/null
+++ b/utils/gui-wrapper-mac.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+set -e
+
+resolution="$1" # e.g. 800x600x24 (width x height x bits_per_pixel)
+shift           # the following arguments are the program to execute and its arguments
+
+bg="$(realpath "build/checkerboard_$(echo "$resolution" | cut -d 'x' -f1-2).png")"
+anim="$(./utils/mktemp.sh -d)"
+
+echo "$anim $resolution $@"
+sleep 2
+   osascript -e "tell application \"Finder\" to set desktop picture to (POSIX file \"$bg\")" \
+|| osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"$bg\""
+sleep 1
+./utils/screenshots-loop.sh mac "$anim" &
+"$@"
+
+touch "$anim/stop-screenshots"
+anim_done=false
+for i in `seq 300`; do if test -e "$anim/anim-done"; then anim_done=true; break; fi; sleep 1; done
+if $anim_done; then echo "anim: done ($*)"; else echo "anim: timeout ($*)"; fi
+if test -e "$anim/anim.gif"; then
+  mv "$anim/anim.gif" "./deploy-screenshots/$(basename "$1" .sh)-anim.gif"
+fi
+
+# Cleanup
+rm -r -- "$anim"
diff --git a/utils/gui-wrapper.sh b/utils/gui-wrapper.sh
index 8a468e4..395b79e 100755
--- a/utils/gui-wrapper.sh
+++ b/utils/gui-wrapper.sh
@@ -5,8 +5,7 @@ set -e
 resolution="$1" # e.g. 800x600x24 (width x height x bits_per_pixel)
 shift           # the following arguments are the program to execute and its arguments
 
-bg="$(./utils/mktemp.sh .xbm)"
-twm_cfg="$(./utils/mktemp.sh .twm.cfg)"
+bg="build/checkerboard_$(echo "$resolution" | cut -d 'x' -f1-2).xbm"
 twm_session_dir="$(./utils/mktemp.sh -d)"
 anim="$(./utils/mktemp.sh -d)"
 
@@ -19,13 +18,13 @@ convert -size "$(echo "$resolution" | cut -d 'x' -f1-2)" \
         -auto-level \
         "$bg"
 
-cat > "$twm_cfg" <<EOF
+cat > "build/twm_cfg" <<EOF
 RandomPlacement
 EOF
 
-echo "$bg $twm_cfg $anim $resolution $@"
+echo "$bg $anim $resolution $@"
 # -fg chocolate -bg coral looks nice too :)
-xvfb-run -a --server-args="-screen 0 ${resolution}" sh -c 'sleep 2; SM_SAVE_DIR="'"$twm_session_dir"'" twm -f "'"$twm_cfg"'" & sleep 1 && xsetroot -bitmap "'"$bg"'" -fg gray75 -bg gray50 && sleep 1 && utils/screenshots-loop.sh "'"$anim"'" & "$@"' utils/gui-wrapper.sh-subshell "$@"
+xvfb-run -a --server-args="-screen 0 ${resolution}" sh -c 'sleep 2; SM_SAVE_DIR="'"$twm_session_dir"'" twm -f "'"build/twm_cfg"'" & sleep 1 && xsetroot -bitmap "'"$bg"'" -fg gray75 -bg gray50 && sleep 1 && ./utils/screenshots-loop.sh x11 "'"$anim"'" & "$@"' ./utils/gui-wrapper.sh-subshell "$@"
 
 touch "$anim/stop-screenshots"
 anim_done=false
@@ -36,5 +35,4 @@ if test -e "$anim/anim.gif"; then
 fi
 
 # Cleanup
-rm -- "$bg" "$twm_cfg"
 rm -r -- "$twm_session_dir" "$anim"
diff --git a/utils/make-travis-key.sh b/utils/make-travis-key.sh
index 0ab6d7f..a7749d7 100755
--- a/utils/make-travis-key.sh
+++ b/utils/make-travis-key.sh
@@ -36,7 +36,7 @@ if ! which travis > /dev/null; then
   gem install travis || echo "Notice: you need the following packages or their equivalent: ruby ruby-dev"
 fi
 
-ssh_dir="$("$(dirname "$0")/mktemp.sh" -d tmp.XXXXXXXXXX_travis-deploy-ssh-keygen)"
+ssh_dir="$("$(dirname "$0")/mktemp.sh" -d)"
 mkdir -m 700 "${ssh_dir}/permissions/"
 ssh-keygen -N '' -f "${ssh_dir}/permissions/travis-deploy-key-id_rsa"
 
diff --git a/utils/mktemp.sh b/utils/mktemp.sh
index 3c0b62a..f0c069d 100755
--- a/utils/mktemp.sh
+++ b/utils/mktemp.sh
@@ -11,16 +11,12 @@ else
   mkdir_opt=''
 fi
 
-if test $# -gt 1; then
-  echo "Usage: $0 [-d] .suffix" >&2
+if test $# -gt 0; then
+  echo "Usage: $0 [-d]" >&2
   exit 1
-elif test $# -eq 1; then
-  suffix="$1"
-else
-  suffix=''
 fi
 
-result="$(mktemp $mkdir_opt "${the_tmp_dir}tmp.XXXXXXXXXX$suffix")"
+result="$(mktemp $mkdir_opt "${the_tmp_dir}/tmp.XXXXXXXXXX")"
 
 # Sanity checks:
 
diff --git a/utils/travis_known_hosts b/utils/travis_known_hosts
new file mode 100644
index 0000000..1bae52b
--- /dev/null
+++ b/utils/travis_known_hosts
@@ -0,0 +1 @@
+github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==