OSDN Git Service

[add] : Initial commit.
[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/en/projects/linux/alter/welcome.php"
14 browser="chromium --start-maximized --app=%s"
15 custombrowser=false
16
17 defaultbrowserlist=(
18     "firefox %s"
19     "chromium --start-maximized --app=%s"
20     "google-chrome  --start-maximized --app=%s"
21 )
22
23 remove () {
24     local _list
25     local _file
26     _list=($(echo "$@"))
27     for _file in "${_list[@]}"; do
28         if [[ -f ${_file} ]]; then
29             rm -f "${_file}"
30         elif [[ -d ${_file} ]]; then
31             rm -rf "${_file}"
32         fi
33     done
34 }
35
36 _help() {
37     echo "Displays the AlterLinux welcome page"
38     echo "usage alterlinux-welcome-page [options]"
39     echo
40     echo " General options:"
41     echo "    -b | --browser <cmd>  Specify the browser command."
42     echo "                          %s will be replaced with the URL"
43     echo "                          Default: ${browser}"
44     echo "    -u | --url <url>      Set the URL."
45     echo "                          Default: ${url}"
46     echo
47     echo "    -l | --live           Opens the page only in a live environment."
48     echo "    -h | --help           This help message and exit."
49 }
50
51 _msg_error() {
52     echo "${@}" >&2
53 }
54
55
56 # Argument analysis and processing
57 options="${@}"
58 _opt_short="b:u:lh"
59 _opt_long="browser:,url:,live,help,alterlive"
60 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
61 if [[ ${?} != 0 ]]; then
62     exit 1
63 fi
64
65 eval set -- "${OPT}"
66 unset OPT
67 unset _opt_short
68 unset _opt_long
69
70 while true; do
71     case ${1} in
72         -b | --browser)
73             browser="${2}"
74             custombrowser=true
75             shift 2
76             ;;
77         -l | --live)
78             checklive=true
79             shift 1
80             ;;
81         -u | --url)
82             url="${2}"
83             shift 2
84             ;;
85         -h | --help)
86             _help
87             shift 1
88             exit 0
89             ;;
90         --alterlive)
91             alterlive=true
92             shift 1
93             ;;
94         --)
95             shift
96             break
97             ;;
98         *)
99             _msg_error "Invalid argument '${1}'"
100             _help
101             exit 1
102             ;;
103     esac
104 done
105
106 if [[ "${checklive}" = true ]]; then
107     if [[ -n $(pacman -Q alterlinux-calamares) ]]; then
108         exit 0
109     fi
110 fi
111
112 if [[ "${custombrowser}" = false ]]; then
113     defaultbrowserlist+=("END_OF_LIST")
114     for ((_browser_count = 0; _browser_count < ${#defaultbrowserlist[@]}; _browser_count++)); do
115         _browser="${defaultbrowserlist[${_browser_count}]}"
116         if [[ -f $(type -P "$(echo ${_browser} | awk '{print $1}')") ]]; then
117             browser="${_browser}"
118             break
119         elif [[ "${_browser}" == "END_OF_LIST" ]]; then
120             _msg_error "No available browser is installed."
121             exit 1
122         fi
123     done
124 fi
125
126 $(printf "${browser}" "${url}")
127
128 if [[ "${alterlive}" = true ]]; then
129     remove ~/.config/autostart/welcome_page.desktop
130 fi