team-ljyzipelo

공개
131줄4.6 KB
1#!/bin/sh
2# 로그인 사용자 권한으로만 이전 선택을 기록하고 조건부 복원함.
3set -eu
4case "${1:-}" in
5 install|remove) action=$1 ;;
6 *) exit 2 ;;
7esac
8if [ "$(id -u)" -eq 0 ]; then
9 echo "기본 앱 처리는 일반 사용자 계정에서 실행해야 합니다." >&2
10 exit 1
11fi
12state="${XDG_STATE_HOME:-$HOME/.local/state}/zipelo-default-apps-v1"
13case "$state" in /*) ;; *) exit 1 ;; esac
14umask 077
15if [ "$action" = install ] && [ ! -e "$state" ] && [ ! -L "$state" ]; then
16 mkdir -p "$(dirname "$state")"
17 mkdir "$state"
18fi
19if [ -L "$state" ] || [ ! -d "$state" ] || [ ! -O "$state" ]; then
20 echo "이전 기본 앱 기록이 없습니다. 파일을 오른쪽 클릭해 [다른 프로그램으로 열기]에서 앱을 선택하세요." >&2
21 exit 0
22fi
23
24# 주 그룹의 키만 읽고 값의 이스케이프를 해석함. 파일 내용을 실행하지 않음.
25desktop_value() {
26 awk -v key="$2" '
27 /^\[/ { entry = ($0 == "[Desktop Entry]"); next }
28 entry && /^[^#=]+=/ {
29 name = substr($0, 1, index($0, "=") - 1)
30 gsub(/^[ \t]+|[ \t]+$/, "", name)
31 if (name != key) next
32 value = substr($0, index($0, "=") + 1)
33 gsub(/^[ \t]+|[ \t]+$/, "", value)
34 result = ""
35 for (i = 1; i <= length(value); i++) {
36 c = substr(value, i, 1)
37 if (c == "\\") {
38 c = substr(value, ++i, 1)
39 if (c == "s") c = " "
40 else if (c == "n") c = "\n"
41 else if (c == "t") c = "\t"
42 else if (c == "r") c = "\r"
43 else if (c != "\\") exit 1
44 }
45 result = result c
46 }
47 print result
48 exit
49 }
50 ' "$1"
51}
52
53executable_available() (
54 case "$1" in
55 /*) [ -f "$1" ] && [ -x "$1" ]; exit $? ;;
56 esac
57 # 내장 명령이나 셸 구문이 아닌 PATH 안의 실행 파일만 검사함.
58 search_path=${PATH:-}
59 while :; do
60 directory=${search_path%%:*}
61 if [ -f "${directory:-.}/$1" ] && [ -x "${directory:-.}/$1" ]; then
62 exit 0
63 fi
64 case "$search_path" in
65 *:*) search_path=${search_path#*:} ;;
66 *) exit 1 ;;
67 esac
68 done
69)
70
71available() {
72 case "$1" in
73 ""|zipelo.desktop|*/*|*\\*|*";"*|*"="*) return 1 ;;
74 *.desktop) ;;
75 *) return 1 ;;
76 esac
77 # 기본 데스크톱 파일만 복원하며 모호한 ID는 수동 선택으로 남김.
78 old_ifs=$IFS
79 IFS=:
80 for directory in "${XDG_DATA_HOME:-$HOME/.local/share}" ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do
81 if [ -f "$directory/applications/$1" ]; then
82 IFS=$old_ifs
83 desktop="$directory/applications/$1"
84 hidden=$(desktop_value "$desktop" Hidden) || return 1
85 [ "$hidden" != true ] || return 1
86 try_exec=$(desktop_value "$desktop" TryExec) || return 1
87 if [ -n "$try_exec" ]; then
88 executable_available "$try_exec" || return 1
89 fi
90 return 0
91 fi
92 done
93 IFS=$old_ifs
94 return 1
95}
96
97for item in zip:application/zip seven:application/x-7z-compressed targz:application/x-compressed-tar gzip:application/gzip tar:application/x-tar; do
98 key=${item%%:*}
99 mime=${item#*:}
100 record="$state/$key"
101 if [ -L "$record" ]; then
102 continue
103 fi
104 current=$(xdg-mime query default "$mime") || continue
105 if [ "$action" = install ]; then
106 # 미완료 기록이나 재설치의 기존 기록을 덮어쓰지 않음.
107 if [ -e "$record" ]; then
108 continue
109 fi
110 (set -C; printf '%s\n' "$current" > "$record") || continue
111 xdg-mime default zipelo.desktop "$mime" || echo "기본 앱 적용 실패: $mime" >&2
112 elif [ -f "$record" ] && [ -O "$record" ]; then
113 previous=$(cat "$record")
114 if [ "$current" = zipelo.desktop ]; then
115 if available "$previous"; then
116 if ! xdg-mime default "$previous" "$mime"; then
117 echo "기본 앱 복원 실패: $mime" >&2
118 continue
119 fi
120 else
121 echo "이전 앱을 복원할 수 없습니다: $mime. 파일을 오른쪽 클릭해 [다른 프로그램으로 열기]에서 앱을 선택하세요." >&2
122 # 이전 앱이 없던 빈 기록은 재시도 대상이 아님.
123 [ -n "$previous" ] && continue
124 fi
125 fi
126 rm -- "$record"
127 fi
128done
129if [ "$action" = remove ]; then
130 rmdir "$state" 2>/dev/null || :
131fi