OSDN Git Service

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