#!/bin/sh # Install (or update) MDZip Studio on Linux. # # curl -fsSL https://mdzip.org/install.sh | sh # # Debian, Ubuntu and their derivatives get the .deb through apt (asks for # sudo). Every other distro gets the AppImage in ~/Applications plus an app # menu entry and the .mdz file type. To pick one yourself: # # curl -fsSL https://mdzip.org/install.sh | sh -s -- --appimage # curl -fsSL https://mdzip.org/install.sh | sh -s -- --deb # # Downloads come from the latest GitHub release of mdzip-project/mdzip-studio # and are checked against the SHA-512 sums published with it. set -eu RELEASES="https://github.com/mdzip-project/mdzip-studio/releases/latest/download" say() { printf '%s\n' "$*"; } warn() { printf 'warning: %s\n' "$*" >&2; } die() { printf 'error: %s\n' "$*" >&2; exit 1; } has() { command -v "$1" >/dev/null 2>&1; } usage() { say "Usage: install.sh [--deb | --appimage]" say " --deb install the .deb with apt (Debian, Ubuntu, Mint, ...)" say " --appimage install the AppImage in ~/Applications (any distro)" say "With no option, the .deb is used when apt is available." } # fetch URL FILE [quiet] fetch() { if has curl; then if [ "${3:-}" = quiet ]; then curl -fsSL --retry 3 -o "$2" "$1" else curl -fL --retry 3 --progress-bar -o "$2" "$1"; fi elif has wget; then if [ "${3:-}" = quiet ]; then wget -q -O "$2" "$1" else wget -O "$2" "$1"; fi else die "curl or wget is needed to download MDZip Studio" fi } # Asset name ending in $2 listed in latest-linux.yml ($1). asset_name() { sed -n 's/^ *- url: *//p' "$1" | tr -d "\"'\r" | sed -n "/$2\$/{p;q;}" } # SHA-512 (base64, as electron-builder writes it) listed for asset $2. asset_sha512() { tr -d "\"'\r" < "$1" | awk -v f="$2" ' $1 == "-" && $2 == "url:" { url = $3; next } $1 == "sha512:" && url == f { print $2; exit }' } # verify FILE BASE64_SHA512 verify() { if [ -z "$2" ]; then warn "no checksum published for $(basename "$1"); skipping the check" return 0 fi if ! has sha512sum || ! has base64 || ! has od; then warn "sha512sum, base64 or od not found; skipping the checksum check" return 0 fi want=$(printf '%s' "$2" | base64 -d | od -An -v -tx1 | tr -d ' \n') got=$(sha512sum "$1" | cut -d' ' -f1) [ "$want" = "$got" ] || die "checksum mismatch for $(basename "$1"); the download is damaged. Nothing was installed." } install_deb() { if [ "$(id -u)" -eq 0 ]; then sudo="" elif has sudo; then sudo="sudo" else die "installing the .deb needs root or sudo (or use --appimage)" fi name=$(asset_name "$yml" '\.deb') [ -n "$name" ] || die "no .deb in the latest release (try --appimage)" say "Downloading $name ..." fetch "$RELEASES/$name" "$tmp/$name" verify "$tmp/$name" "$(asset_sha512 "$yml" "$name")" # apt drops privileges to read local files; let it. chmod 755 "$tmp" chmod 644 "$tmp/$name" say "Installing with apt${sudo:+ (sudo may ask for your password)} ..." if has apt-get; then # Retry once with fresh package lists, in case Studio's dependencies # point at versions the local lists no longer have. $sudo apt-get install -y "$tmp/$name" < /dev/null || { say "Refreshing package lists and retrying ..." $sudo apt-get update < /dev/null $sudo apt-get install -y "$tmp/$name" < /dev/null } else $sudo dpkg -i "$tmp/$name" < /dev/null fi say "" say "MDZip Studio $version is installed. Open it from your app menu." say "It updates itself. To remove it: sudo apt remove mdzip-studio" } install_appimage() { name=$(asset_name "$yml" '\.AppImage') [ -n "$name" ] || die "no AppImage in the latest release" dest="${MDZIP_STUDIO_DIR:-$HOME/Applications}" app="$dest/MDZip-Studio.AppImage" data="${XDG_DATA_HOME:-$HOME/.local/share}" say "Downloading $name ..." fetch "$RELEASES/$name" "$tmp/$name" verify "$tmp/$name" "$(asset_sha512 "$yml" "$name")" mkdir -p "$dest" chmod 755 "$tmp/$name" mv -f "$tmp/$name" "$app" # Icon for the menu entry, taken from the AppImage itself (no FUSE needed). # The top-level .png is usually a symlink into usr/share/icons, so follow # it and extract the file it points to. icon="" if (cd "$tmp" && "$app" --appimage-extract '*.png' >/dev/null 2>&1); then for png in "$tmp"/squashfs-root/*.png; do if [ -L "$png" ] && has readlink; then target=$(readlink "$png") (cd "$tmp" && "$app" --appimage-extract "$target" >/dev/null 2>&1) || true png="$tmp/squashfs-root/$target" fi if [ -f "$png" ]; then mkdir -p "$data/icons" cp -f "$png" "$data/icons/mdzip-studio.png" icon="$data/icons/mdzip-studio.png" break fi done fi mkdir -p "$data/applications" "$data/mime/packages" cat > "$data/applications/mdzip-studio.desktop" < "$data/mime/packages/mdzip-studio.xml" <<'EOF' MDZip Document EOF if has update-mime-database; then update-mime-database "$data/mime" >/dev/null 2>&1 || true; fi if has update-desktop-database; then update-desktop-database "$data/applications" >/dev/null 2>&1 || true; fi if has xdg-mime; then xdg-mime default mdzip-studio.desktop application/vnd.mdzip >/dev/null 2>&1 || true; fi say "" say "MDZip Studio $version is installed: $app" say "Open it from your app menu. It updates itself." # AppImages need FUSE 2 to start. Only warn when we can tell it's missing. for ldconfig in ldconfig /sbin/ldconfig /usr/sbin/ldconfig; do if has "$ldconfig"; then if ! "$ldconfig" -p 2>/dev/null | grep -q 'libfuse\.so\.2'; then say "" warn "libfuse2 looks missing, and the AppImage needs it to start." say " Install it with your package manager, for example:" say " Fedora: sudo dnf install fuse-libs" say " Arch: sudo pacman -S fuse2" say " openSUSE: sudo zypper install libfuse2" fi break fi done say "" say "To remove it, delete $app and" say " $data/applications/mdzip-studio.desktop" say " $data/mime/packages/mdzip-studio.xml" } main() { format="" for arg in "$@"; do case "$arg" in --deb) format=deb ;; --appimage) format=appimage ;; -h|--help) usage; exit 0 ;; *) usage >&2; die "unknown option: $arg" ;; esac done [ "$(uname -s)" = Linux ] || die "this installer is for Linux. Windows and macOS: see https://mdzip.org/studio.html" case "$(uname -m)" in x86_64|amd64) ;; *) die "MDZip Studio is only built for x86_64 so far (this machine is $(uname -m))" ;; esac if [ -z "$format" ]; then if has apt-get && has dpkg; then format=deb; else format=appimage; fi fi tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT trap 'exit 1' INT TERM yml="$tmp/latest-linux.yml" fetch "$RELEASES/latest-linux.yml" "$yml" quiet || die "couldn't reach GitHub to find the latest release" version=$(sed -n 's/^version: *//p' "$yml" | tr -d "\"'\r" | head -n 1) [ -n "$version" ] || die "couldn't read the latest version from GitHub" say "Installing MDZip Studio $version ($format) ..." case "$format" in deb) install_deb ;; appimage) install_appimage ;; esac } # Everything above is only definitions, so a download cut short runs nothing. main "$@"