OSDN Git Service

74805c801ffc9ac677ea4d40996c4e315691ac15
[alterlinux/fascode-live-tools.git] / alterlinux-desktop-file / alterlinux-desktop-file
1 #!/usr/bin/env bash
2 # Yamada Hayao
3 # Twitter: @Hayao0819
4 # Email  : hayao@fascode.net
5 #
6 # (c) 2019-2021 Fascode Network.
7 #
8
9 set -e
10
11 alterlive=false
12 force=false
13 checklive=false
14
15 remove () {
16     local _list
17     local _file
18     _list=($(echo "$@"))
19     for _file in "${_list[@]}"; do
20         if [[ -f ${_file} ]]; then
21             rm -f "${_file}"
22         elif [[ -d ${_file} ]]; then
23             rm -rf "${_file}"
24         fi
25     done
26 }
27
28 _help() {
29     echo "usage ${0} [options]"
30     echo
31     echo " General options:"
32     echo "    -f | --force        Force overwriting."
33     echo "    -h | --help         This help message and exit."
34     echo "    -l | --live         Create files only in a live environment."
35     echo "                        Whether it is a live environment or not is determined"
36     echo "                        by the presence or absence of the installer."
37     echo
38     echo "         --alterlive    Remove the file for live session"
39     echo "                        If you specify --alterlive, --live is also specified automatically."
40 }
41
42 # Argument analysis and processing
43 options="${@}"
44 opt_short="fhl"
45 opt_long="force,live,help,alterlive"
46 OPT="$(getopt -o ${opt_short} -l ${opt_long} -- "${@}")"
47 if (( "${?}" != 0 )); then
48     exit 1
49 fi
50
51 eval set -- "${OPT}"
52 unset OPT
53 unset opt_short
54 unset opt_long
55
56 while true; do
57     case "${1}" in
58         -f | --force)
59             force=true
60             shift 1
61             ;;
62         -l | --live)
63             checklive=true
64             shift 1
65             ;;
66         -h | --help)
67             _help
68             shift 1
69             exit 0
70             ;;
71         --alterlive)
72             alterlive=true
73             #checklive=true
74             shift 1
75             ;;
76         --)
77             shift
78             break
79             ;;
80         *)
81             echo "Invalid argument '${1}'" >&2
82             _help
83             exit 1
84             ;;
85     esac
86 done
87
88 if [[ "${alterlive}" = true ]]; then
89     remove "${HOME}/.config/autostart/genicon.desktop"
90 fi
91
92 if [[ "${checklive}" = true ]]; then
93     if ! pacman -Qq alterlinux-calamares 1> /dev/null 2> /dev/null; then
94         exit 0
95     fi
96 fi
97
98
99 # ~/Desktopに相当するディレクトリを返します
100 get_desktop_dir() {
101     local _user_config_dir _desktop_dir
102     if [[ -v XDG_CONFIG_HOME ]]; then
103         _user_config_dir="${XDG_CONFIG_HOME}"
104     else
105         _user_config_dir="${HOME}/.config"
106     fi
107     if [[ -f "${_user_config_dir}/user-dirs.dirs" ]]; then
108         source "${_user_config_dir}/user-dirs.dirs"
109     fi
110     if [[ -v XDG_DESKTOP_DIR ]]; then
111         _desktop_dir="${XDG_DESKTOP_DIR}"
112     else
113         _desktop_dir="${HOME}/Desktop"
114     fi
115     echo -n "${_desktop_dir}"
116 }
117
118 #copy <コピー元> <コピー先>
119 copy() {
120     if [[ ! -f "${1}" ]]; then
121         echo "${1} was not found" >&2
122         exit 1
123     fi
124     if [[ -f "${2}/$(basename "${1}")" ]] || [[ -f "${2}" ]] && [[ "${force}" = false ]]; then
125         echo "File already exists" >&2
126         exit 1
127     fi
128
129     cp "${1}" "${2}"
130 }
131
132 desktop_dir="$(get_desktop_dir)"
133
134 # calamaresのアイコン
135 if pacman -Qq alterlinux-calamares 1> /dev/null 2> /dev/null; then
136     source_file="/usr/share/alterlinux/desktop-file/calamares.desktop"
137     desktop_icon="${desktop_dir}/$(basename "${source_file}")"
138     copy "${source_file}" "${desktop_dir}"
139     os_name="$(
140         if [[ -f "/etc/os-release" ]]; then
141             source "/etc/os-release"
142             echo -n "${NAME}"
143         else
144             echo -n "Alter Linux"
145         fi
146     )"
147     sed -i "s/%OS_NAME%/${os_name}/g" "${desktop_icon}"
148
149     # Set permission for gnome desktop
150     # https://gitlab.com/rastersoft/desktop-icons-ng/-/issues/111
151     gio set "${desktop_icon}" metadata::trusted true
152 fi
153
154
155 # welcome-page
156 source_file="/usr/share/alterlinux/desktop-file/welcome-to-alter.desktop"
157 desktop_icon="${desktop_dir}/$(basename "${source_file}")"
158 copy "${source_file}" "${desktop_dir}"
159 gio set "${desktop_icon}" metadata::trusted true