OSDN Git Service

4410bfed91dfbfb430e13ed551cf28aa7bd57f7d
[alterlinux/fascode-live-tools.git] / alterlinux-welcome-page / alterlinux-welcome-page
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 checklive=false
12 alterlive=false
13 url="https://alter.fascode.net/alter-welcome.php"
14 browser="chromium --start-maximized --app=%s"
15 custombrowser=false
16 nourlmode=false
17
18 defaultbrowserlist=(
19     "microsoft-edge-dev --start-maximized --app=%s"
20     "vivaldi-stable --start-maximized --app=%s"
21     "brave --start-maximized --app=%s"
22     "firefox-developer-edition -url %s & xdotool search --sync --onlyvisible --class 'Firefox' windowactivate key F11"
23     "firefox -url %s & xdotool search --sync --onlyvisible --class 'Firefox' windowactivate key F11"
24     "chromium --start-maximized --app=%s"
25     "google-chrome  --start-maximized --app=%s"
26 )
27
28 remove () {
29     local _list
30     local _file
31     _list=($(echo "$@"))
32     for _file in "${_list[@]}"; do
33         if [[ -f ${_file} ]]; then
34             rm -f "${_file}"
35         elif [[ -d ${_file} ]]; then
36             rm -rf "${_file}"
37         fi
38     done
39 }
40
41 _help() {
42     echo "Displays the AlterLinux welcome page"
43     echo "usage alterlinux-welcome-page [options]"
44     echo
45     echo " General options:"
46     echo "    -b | --browser <cmd>  Specify the browser command."
47     echo "                          %s will be replaced with the URL"
48     echo "    -u | --url <url>      Set the URL."
49     echo "                          Default: ${url}"
50     echo "    -l | --live           Opens the page only in a live environment."
51     echo "                          Whether it is a live environment or not is determined"
52     echo "                          by the presence or absence of the installer."
53     echo "    --alterlive           Delete the startup file for the live environment."
54     echo "    -h | --help           This help message and exit."
55     echo
56     echo "%s is replaces with the URL"
57     echo " Browser list:"
58     local _browser _browser_count
59     for ((_browser_count = 0; _browser_count < ${#defaultbrowserlist[@]}; _browser_count++)); do
60         echo "    ${defaultbrowserlist[${_browser_count}]}"
61     done
62 }
63
64 _msg_error() {
65     echo "${@}" >&2
66 }
67
68 _msg_warn() {
69     echo "${@}" >&2
70 }
71
72 # Argument analysis and processing
73 options="${@}"
74 _opt_short="b:u:lh"
75 _opt_long="browser:,url:,live,help,alterlive,aobuta"
76 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
77 if [[ ${?} != 0 ]]; then
78     exit 1
79 fi
80
81 eval set -- "${OPT}"
82 unset OPT
83 unset _opt_short
84 unset _opt_long
85
86 while true; do
87     case ${1} in
88         -b | --browser)
89             browser="${2}"
90             custombrowser=true
91             shift 2
92             ;;
93         -l | --live)
94             checklive=true
95             shift 1
96             ;;
97         -u | --url)
98             url="${2}"
99             shift 2
100             ;;
101         -h | --help)
102             _help
103             shift 1
104             exit 0
105             ;;
106         --aobuta)
107             url="https://ao-buta.com/"
108             shift 1
109             ;;
110         --alterlive)
111             alterlive=true
112             shift 1
113             ;;
114         --)
115             shift
116             break
117             ;;
118         *)
119             _msg_error "Invalid argument '${1}'"
120             _help
121             exit 1
122             ;;
123     esac
124 done
125
126 # ライブ環境から実行されているかチェックします
127 check_livecd(){
128     if [[ -d "/run/archiso" ]] || grep "^archisobasedir=" "/proc/cmdline" 2> /dev/null 1>&2; then
129         return 0
130     fi
131     return 1
132 }
133
134 if [[ "${checklive}" = true ]] && check_livecd; then
135     exit 0
136 fi
137
138 # ブラウザが指定されていないなら一覧から自動で検出する
139 if [[ "${custombrowser}" = false ]]; then
140     defaultbrowserlist+=("END_OF_LIST")
141     for ((_browser_count = 0; _browser_count < ${#defaultbrowserlist[@]}; _browser_count++)); do
142         _browser="${defaultbrowserlist[${_browser_count}]}"
143         if [[ -f $(type -P "$(echo ${_browser} | awk '{print $1}')") ]]; then
144             browser="${_browser}"
145             break
146         elif [[ "${_browser}" == "END_OF_LIST" ]]; then
147             _msg_error "No available browser is installed."
148             exit 1
149         fi
150     done
151 elif [[ -z "$(echo "${browser}" | grep "%s")" ]]; then
152     # -bで%sが指定されていないならコマンドの後にURLを付ける
153     nourlmode=true
154 fi
155
156 if [[ "${nourlmode}" = true ]]; then
157     _msg_warn "Directly executed with URL as an argument because %s was not specified."
158     eval ${browser} ${url} &
159 else
160     eval $(printf "${browser}" "${url}") &
161 fi
162
163 if [[ "${alterlive}" = true ]]; then
164     remove "${HOME}/.config/autostart/welcome_page.desktop"
165 fi