start-local-collaboration

 1#!/bin/bash
 2
 3set -e
 4
 5if [[ -z "$GITHUB_TOKEN" ]]; then
 6  cat <<-MESSAGE
 7Missing \`GITHUB_TOKEN\` environment variable. This token is needed
 8for fetching your GitHub identity from the command-line.
 9
10Create an access token here: https://github.com/settings/tokens
11Then edit your \`~/.zshrc\` (or other shell initialization script),
12adding a line like this:
13
14    export GITHUB_TOKEN="(the token)"
15
16MESSAGE
17  exit 1
18fi
19
20# Start one Zed instance as the current user and a second instance with a different user.
21username_1=$(curl -sH "Authorization: bearer $GITHUB_TOKEN" https://api.github.com/user | jq -r .login)
22username_2=nathansobo
23if [[ $username_1 == $username_2 ]]; then
24  username_2=as-cii
25fi
26
27# Make each Zed instance take up half of the screen.
28resolution_line=$(system_profiler SPDisplaysDataType | grep Resolution | head -n1)
29screen_size=($(echo $resolution_line | egrep -o '\s*(\d+)\s*x\s*(\d+).*)' | egrep -o '[0-9]+'))
30scale_factor=1
31if [[ $resolution_line =~ Retina ]]; then scale_factor=2; fi
32width=$(expr ${screen_size[0]} / 2 / $scale_factor)
33height=${screen_size[1] / $scale_factor}
34y=0
35
36position_1=0,${y}
37position_2=${width},${y}
38
39# Uncomment the following for debugging purposes.
40# echo "Resolution line: $resolution_line"
41# echo "Screen size: $screen_size"
42# echo "Screen size 0: ${screen_size[0]}"
43# echo "Screen size 1: ${screen_size[1]}"
44# echo "Scale factor: $scale_factor"
45# echo "Width: $width"
46# echo "Height: $height"
47# echo "Position 1: $position_1"
48# echo "Position 2: $position_2"
49
50# Authenticate using the collab server's admin secret.
51export ZED_STATELESS=1
52export ZED_ADMIN_API_TOKEN=secret
53export ZED_SERVER_URL=http://localhost:8080
54export ZED_WINDOW_SIZE=${width},${height}
55
56cargo build
57sleep 0.5
58
59# Start the two Zed child processes. Open the given paths with the first instance.
60trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
61ZED_IMPERSONATE=${username_1} ZED_WINDOW_POSITION=${position_1} target/debug/Zed $@ &
62ZED_IMPERSONATE=${username_2} ZED_WINDOW_POSITION=${position_2} target/debug/Zed &
63wait