1#!/usr/bin/env sh
  2set -eu
  3
  4# Uninstalls Zed that was installed using the install.sh script
  5
  6check_remaining_installations() {
  7    platform="$(uname -s)"
  8    if [ "$platform" = "Darwin" ]; then
  9        # Check for any Zed variants in /Applications
 10        remaining=$(ls -d /Applications/Zed*.app 2>/dev/null | wc -l)
 11        [ "$remaining" -eq 0 ]
 12    else
 13        # Check for any Zed variants in ~/.local
 14        remaining=$(ls -d "$HOME/.local/zed"*.app 2>/dev/null | wc -l)
 15        [ "$remaining" -eq 0 ]
 16    fi
 17}
 18
 19prompt_remove_preferences() {
 20    printf "Do you want to keep your Zed preferences? [Y/n] "
 21    read -r response
 22    case "$response" in
 23        [nN]|[nN][oO])
 24            rm -rf "$HOME/.config/zed"
 25            echo "Preferences removed."
 26            ;;
 27        *)
 28            echo "Preferences kept."
 29            ;;
 30    esac
 31}
 32
 33main() {
 34    platform="$(uname -s)"
 35    channel="${ZED_CHANNEL:-stable}"
 36
 37    if [ "$platform" = "Darwin" ]; then
 38        platform="macos"
 39    elif [ "$platform" = "Linux" ]; then
 40        platform="linux"
 41    else
 42        echo "Unsupported platform $platform"
 43        exit 1
 44    fi
 45
 46    "$platform"
 47
 48    echo "Zed has been uninstalled"
 49}
 50
 51linux() {
 52    suffix=""
 53    if [ "$channel" != "stable" ]; then
 54        suffix="-$channel"
 55    fi
 56
 57    appid=""
 58    db_suffix="stable"
 59    case "$channel" in
 60      stable)
 61        appid="dev.zed.Zed"
 62        db_suffix="stable"
 63        ;;
 64      nightly)
 65        appid="dev.zed.Zed-Nightly"
 66        db_suffix="nightly"
 67        ;;
 68      preview)
 69        appid="dev.zed.Zed-Preview"
 70        db_suffix="preview"
 71        ;;
 72      dev)
 73        appid="dev.zed.Zed-Dev"
 74        db_suffix="dev"
 75        ;;
 76      *)
 77        echo "Unknown release channel: ${channel}. Using stable app ID."
 78        appid="dev.zed.Zed"
 79        db_suffix="stable"
 80        ;;
 81    esac
 82
 83    # Remove the app directory
 84    rm -rf "$HOME/.local/zed$suffix.app"
 85
 86    # Remove the binary symlink
 87    rm -f "$HOME/.local/bin/zed"
 88
 89    # Remove the .desktop file
 90    rm -f "$HOME/.local/share/applications/${appid}.desktop"
 91
 92    # Remove the database directory for this channel
 93    rm -rf "$HOME/.local/share/zed/db/0-$db_suffix"
 94
 95    # Remove socket file
 96    rm -f "$HOME/.local/share/zed/zed-$db_suffix.sock"
 97
 98    # Remove the entire Zed directory if no installations remain
 99    if check_remaining_installations; then
100        rm -rf "$HOME/.local/share/zed"
101        prompt_remove_preferences
102    fi
103
104    rm -rf $HOME/.zed_server
105}
106
107macos() {
108    app="Zed.app"
109    db_suffix="stable"
110    app_id="dev.zed.Zed"
111    case "$channel" in
112      nightly)
113        app="Zed Nightly.app"
114        db_suffix="nightly"
115        app_id="dev.zed.Zed-Nightly"
116        ;;
117      preview)
118        app="Zed Preview.app"
119        db_suffix="preview"
120        app_id="dev.zed.Zed-Preview"
121        ;;
122      dev)
123        app="Zed Dev.app"
124        db_suffix="dev"
125        app_id="dev.zed.Zed-Dev"
126        ;;
127    esac
128
129    # Remove the app bundle
130    if [ -d "/Applications/$app" ]; then
131        rm -rf "/Applications/$app"
132    fi
133
134    # Remove the binary symlink
135    rm -f "$HOME/.local/bin/zed"
136
137    # Remove the database directory for this channel
138    rm -rf "$HOME/Library/Application Support/Zed/db/0-$db_suffix"
139
140    # Remove app-specific files and directories
141    rm -rf "$HOME/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/$app_id.sfl"*
142    rm -rf "$HOME/Library/Caches/$app_id"
143    rm -rf "$HOME/Library/HTTPStorages/$app_id"
144    rm -rf "$HOME/Library/Preferences/$app_id.plist"
145    rm -rf "$HOME/Library/Saved Application State/$app_id.savedState"
146
147    # Remove the entire Zed directory if no installations remain
148    if check_remaining_installations; then
149        rm -rf "$HOME/Library/Application Support/Zed"
150        rm -rf "$HOME/Library/Logs/Zed"
151
152        prompt_remove_preferences
153    fi
154
155    rm -rf $HOME/.zed_server
156}
157
158main "$@"