OSDN Git Service

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