From 96358830fbc1040d8547ca5cd3da26cdbe84bf2d Mon Sep 17 00:00:00 2001
From: syui <syui@syui.ai>
Date: Mon, 24 Feb 2025 13:38:55 +0900
Subject: [PATCH] fix install.zsh

---
 install.zsh       | 132 +++++++++++++++++++++++++++++++++-------------
 scpt/at.zsh       |   2 +-
 scpt/src/func.zsh |  36 +++++++++----
 3 files changed, 123 insertions(+), 47 deletions(-)

diff --git a/install.zsh b/install.zsh
index 18ad070..f1358f8 100755
--- a/install.zsh
+++ b/install.zsh
@@ -1,23 +1,51 @@
 #!/bin/zsh
 
-d=${0:a:h}
+# ./install.zsh $HOST
 
-repos=(
-	https://github.com/did-method-plc/did-method-plc
-	https://github.com/bluesky-social/indigo
-	https://github.com/bluesky-social/atproto
-	https://github.com/bluesky-social/social-app
-	https://github.com/bluesky-social/feed-generator
-)
-
-function run-env() {
-	host=syu.is
+function at-repos-env() {
+	host=$1
+	if [ -z "$1" ];then
+		host=syu.is
+	fi
+	did=did:plc:6qyecktefllvenje24fcxnie
+	icon=https://git.syui.ai/ai/at/raw/branch/main/icons/Logotype.tsx
+	repos=(
+		https://github.com/did-method-plc/did-method-plc
+		https://github.com/bluesky-social/indigo
+		https://github.com/bluesky-social/atproto
+		https://github.com/bluesky-social/social-app
+		https://github.com/bluesky-social/feed-generator
+	)
+	d=${0:a:h}
+	dh=${0:a:h:h}
 	name=${host%%.*}
 	domain=${host##*.}
-	dh=${0:a:h:h}
 }
 
-function run-dl() {
+function at-repos-json() {
+	f=~/.config/atproto/token.json
+	j="{ \"did\": \"did:plc:6qyecktefllvenje24fcxnie\", \"didDoc\": { \"service\": [ { \"serviceEndpoint\": \"https://syu.is\" } ] }, \"handle\": \"ai.syu.is\", \"accessJwt\": \"xxx\" }"
+	if [ ! -f "$f" ];then
+		mkdir -p ~/.config/atproto
+		echo $j >> $f
+	fi
+	echo $f
+}
+
+function at-repos-token() {
+	at-repos-json
+	if [ -z "$host" ] && [ -f $f ];then
+		host=`cat $f|jq -r ".didDoc.service.[].serviceEndpoint"`
+		handle=`cat $f|jq -r ".handle"`
+		did=`cat $f|jq -r ".did"`
+		token=`cat $f|jq -r ".token"`
+		host=${host##*/}
+	fi
+	name=${host%%.*}
+	domain=${host##*.}
+}
+
+function at-repos-clone() {
 	mkdir -p $d/repos
 	cd $d/repos
 	for ((i=1; i<=${#repos}; i++)); do
@@ -27,37 +55,49 @@ function run-dl() {
 			git clone $repo
 		fi
 	done
-	if [ ! -f $d/repos/feed-generator/Dockerfile ];then
+	if [ ! -f $d/repos/feed-generator/Dockerfile ] && [ -f $d/docker/feed/Dockerfile ];then
 		cp -rf $d/docker/feed/Dockerfile $d/repos/feed-generator/
 	fi
 }
 
-function run-update() {
+function at-repos-pull() {
 	cd $d/repos
 	for ((i=1; i<=${#repos}; i++)); do
 		repo=${repos[$i]}
 		echo $repo
 		if [ -d $d/repos/${repo##*/} ];then
 			cd $d/repos/${repo##*/}
-			git pull
+			if ! git pull;then
+				cd $d/repos
+				rm -rf $d/repos/${repo##*/}
+				at-repos-clone
+			fi
 		fi
 	done
 	cd $d
-	echo "docker compose build && docker compose up -d"
 }
 
-function run-dl-icon() {
+function at-repos-social-app-icon() {
 	curl -sL https://raw.githubusercontent.com/bluesky-social/social-app/main/src/view/icons/Logotype.tsx -o $d/repos/social-app/src/view/icons/Logotype.tsx
+	if [ -d $d/icons ];then
+		mkdir -p $d/icons
+	fi
 	cp -rf $d/repos/social-app/src/view/icons/Logotype.tsx $d/icons/
 }
 
+function at-repos-social-app-icon-origin() {
+	curl -sL $icon -o $d/icons/Logotype.tsx
+}
+
+function at-repos-social-app-write() {
+	did_admin=did:plc:z72i7hdynmk6r22z27h6tvur
 
-function web-write() {
 	dt=$d/repos/social-app/src
 	cd $dt
-	grep -R bsky.social .|cut -d : -f 1|sort -u|xargs sed -i "s/bsky.social/syu.is/g"
-	grep -R bsky.app .|cut -d : -f 1|sort -u|xargs sed -i "s/bsky.app/web.syu.is/g"
-	grep -R public.api.syu.is ./lib/constants.ts | cut -d : -f 1|sort -u|xargs sed -i "s/public.api/bsky/g"
+	grep -R bsky.social .|cut -d : -f 1|sort -u|xargs sed -i "s/bsky.social/${host}/g"
+	grep -R bsky.app .|cut -d : -f 1|sort -u|xargs sed -i "s/bsky.app/web.${host}/g"
+	f=$dt/lib/constants.ts
+	sed -i "s/public.api.web/bsky/g" $f
 	f=$dt/view/icons/Logotype.tsx
 	o=$d/icons/Logotype.tsx
 	cp -rf $o $f
@@ -65,22 +105,42 @@ function web-write() {
 	f=$dt/view/com/util/UserAvatar.tsx
 	curl -sL https://raw.githubusercontent.com/bluesky-social/social-app/refs/heads/main/src/view/com/util/UserAvatar.tsx -o $f
 	sed -i "s#/img/avatar/plain/#https://cdn.bsky.app/img/avatar/plain/#g" $f
-	sed -i "s#/img/avatar_thumbnail/plain/#https://bsky.syu.is/img/avatar/plain/#g" $f
+	sed -i "s#/img/avatar_thumbnail/plain/#https://bsky.${host}/img/avatar/plain/#g" $f
 	sed -i "s#source={{uri: avatar}}#source={{ uri: hackModifyThumbnailPath(avatar, 1 > 0), }}#g" $f
-
-	did_bskyadmin=did:plc:z72i7hdynmk6r22z27h6tvur
-	# curl -sL "syu.is/xrpc/com.atproto.repo.describeRepo?repo=ai.syu.is" |jq -r .did
-	did_ai=did:plc:6qyecktefllvenje24fcxnie
-	grep -R $did_bskyadmin .|cut -d : -f 1|sort -u|xargs sed -i "s/$did_bskyadmin/$did_ai/g"
+	grep -R $did_admin .|cut -d : -f 1|sort -u|xargs sed -i "s/${did_admin}/${did}/g"
 }
 
-run-env
-run-dl
-case $1 in
-	update)
-		run-update
-		;;
-	web)
-		web-write
+function at-repos-bsky-patch() {
+	f=$d/repos/atproto/services/bsky/api.js
+	curl -sL https://raw.githubusercontent.com/bluesky-social/atproto/refs/heads/main/services/bsky/api.js -o $f
+	d_=$d/repos/atproto
+	p_=$d_/105-atproto-services-for-docker.diff
+	cd $d_
+	# https://github.com/itaru2622/bluesky-selfhost-env/blob/master/patching/105-atproto-services-for-docker.diff
+	curl -sLO https://raw.githubusercontent.com/itaru2622/bluesky-selfhost-env/refs/heads/master/patching/105-atproto-services-for-docker.diff
+	echo "applying patch: under ${d_} for ${p_}"
+	pushd ${d_}
+	patch -p1 < ${p_}
+	popd
+}
+
+function at-repos-docker() {
+	cd $d
+	docker compose build && docker compose up -d
+}
+
+at-repos-env
+at-repos-clone
+at-repos-pull
+at-repos-social-app-icon
+at-repos-social-app-icon-origin
+at-repos-social-app-write
+at-repos-bsky-patch
+
+echo "[y]docker compose build && up"
+read key
+case $key in
+	[yY])
+		at-repos-docker
 		;;
 esac
diff --git a/scpt/at.zsh b/scpt/at.zsh
index c947eb8..af1448e 100755
--- a/scpt/at.zsh
+++ b/scpt/at.zsh
@@ -8,7 +8,7 @@ source $d/src/func.zsh
 at-env
 case $1 in
 	version|v)
-		at-pds-version
+		at-version $2
 		;;
 	uri|u)
 		at-uri $2
diff --git a/scpt/src/func.zsh b/scpt/src/func.zsh
index fffbd61..df9ea6a 100644
--- a/scpt/src/func.zsh
+++ b/scpt/src/func.zsh
@@ -27,17 +27,33 @@ function at-unset() {
 	unset t password token refresh
 }
 
-function at-pds-version-env() {
-	title="atproto pds version"
-	latest=https://raw.githubusercontent.com/bluesky-social/atproto/refs/heads/main/packages/pds/package.json
-	pds=( bsky.social syu.is boobee.blue socl.is )
+function at-version-env() {
+	case $1 in
+		bsky)
+			tag=$1
+			list=( public.api.bsky.app bsky.syu.is )
+			;;
+		ozone)
+			tag=$1
+			list=( ozone.bsky.social ozone.syu.is )
+			;;
+		*)
+			tag=pds
+			list=( bsky.social syu.is boobee.blue socl.is )
+			;;
+	esac
+	title="atproto $tag version"
+	latest=https://raw.githubusercontent.com/bluesky-social/atproto/refs/heads/main/packages/$tag/package.json
+
+	# bgs=( bsky.network bgs.syu.is )
+	# plc=( plc.directory plc.syu.is )
 }
 
-function at-pds-version-json() {
+function at-version-json() {
 	latest=`curl -sL $latest |jq -r .version`
 	echo "[{\"title\":\"$title\",\"latest\":\"$latest\"},["
-	e=${pds[@]: -1}
-	for i in $pds; do
+	e=${list[@]: -1}
+	for i in $list; do
 		t=`curl -sL ${i}/xrpc/_health |jq -r .version`
 		echo "{\"$i\":\"$t\"}"
 		if [ "$e" != "$i" ];then
@@ -47,9 +63,9 @@ function at-pds-version-json() {
 	echo "]]"
 }
 
-function at-pds-version() {
-	at-pds-version-env
-	at-pds-version-json |jq .
+function at-version() {
+	at-version-env $1
+	at-version-json |jq .
 }
 
 function at-uri() {