OSDN Git Service

[fix] : FIxed init simulation
[alterlinux/fascode-live-tools.git] / alterlinux-gtk-bookmarks / alterlinux-gtk-bookmarks
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
10 set -e
11
12 force=false
13 alterlive=false
14 simulation=false
15
16 remove () {
17     local _list
18     local _file
19     _list=($(echo "$@"))
20     for _file in "${_list[@]}"; do
21         if [[ -f ${_file} ]]; then
22             rm -f "${_file}"
23         elif [[ -d ${_file} ]]; then
24             rm -rf "${_file}"
25         fi
26     done
27 }
28
29 _help() {
30     echo "usage ${0} [options] [command]"
31     echo
32     echo " General options:"
33     echo "    -f | --force          Force overwriting"
34     echo "    -s | --simulation     Enable sumulation"
35     echo "    -h | --help           This help message and exit"
36     echo
37     echo " General command:"
38     echo "    add <dir>             Add items to the sidebar"
39     echo "    delete                Delete all sidebar items"
40     echo "    init                  Initializes the sidebar"
41     echo "    help                  This help message and exit"
42 }
43
44 output() {
45     if [[ "${simulation}" = true ]]; then
46         echo "${@}"
47     else
48         echo "${@}" >> "${HOME}/.config/gtk-3.0/bookmarks"
49     fi
50 }
51
52 _msg_error() {
53     echo "${@}" >&2
54 }
55
56 prepare() {
57     if [[ ! -d "${HOME}/.config/gtk-3.0/" ]]; then
58         mkdir -p "${HOME}/.config/gtk-3.0/"
59     fi
60     if [[ ! -f "${HOME}/.config/gtk-3.0/bookmarks" ]]; then
61         touch "${HOME}/.config/gtk-3.0/bookmarks"
62     fi
63 }
64
65 add() {
66     prepare
67     local dir
68     for dir in ${@}; do
69         if [[ ! -d "${dir}" ]]; then
70             _msg_error "${dir} does not exist."
71             exit 1
72         else
73             output "file://${dir}"
74         fi
75     done
76 }
77
78 init() {
79     remove "${HOME}/.config/gtk-3.0/bookmarks"
80
81     prepare
82
83     source "${HOME}/.config/user-dirs.dirs"
84
85     output "file://${XDG_DOCUMENTS_DIR} Documents"
86     output "file://${XDG_DOWNLOAD_DIR} Downloads"
87     output "file://${XDG_MUSIC_DIR} Music"
88     output "file://${XDG_PICTURES_DIR} Pictures"
89     output "file://${XDG_VIDEOS_DIR} Videos"
90 }
91
92
93
94 # Argument analysis and processing
95 options="${@}"
96 _opt_short="fhts"
97 _opt_long="force,help,alterlive,t-mart,simulation"
98 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
99 if [[ ${?} != 0 ]]; then
100     exit 1
101 fi
102
103 eval set -- "${OPT}"
104 unset OPT
105 unset _opt_short
106 unset _opt_long
107
108
109 while true; do
110     case ${1} in
111         -s | --simulation)
112             simulation=true
113             shift 1
114             ;;
115         -f | --force)
116             force=true
117             shift 1
118             ;;
119         -h | --help)
120             _help
121             shift 1
122             exit 0
123             ;;
124         --alterlive)
125             alterlive=true
126             shift 1
127             ;;
128         -t | --t-mart)
129             echo "さすが店長、青春ブタ野郎だね"
130             shift 1
131             exit 0
132             ;;
133         --)
134             shift
135             break
136             ;;
137         *)
138             _msg_error "Invalid argument '${1}'"
139             _help
140             exit 1
141             ;;
142     esac
143 done
144
145 mode="${1}"
146
147 case "${1}" in
148     add) 
149         shift 1
150         if [[ -z "${*}" ]]; then
151             _msg_error "Please specify a directory."
152             exit 1
153         else
154             add "${@}"
155         fi
156         exit 0
157         ;;
158     delete)
159         remove "${HOME}/.config/gtk-3.0/bookmarks"
160         exit 0
161         ;;
162     init)
163         if [[ -f "${HOME}/.config/gtk-3.0/bookmarks" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]]; then
164             _msg_error "The sidebar already exists. Use -f to force initialization."
165             exit 1
166         else
167             init
168         fi
169         exit 0
170         ;;
171     help)
172         _help
173         exit 0
174         ;;
175     *)
176         _msg_error "Please specify a command."
177         exit 1
178         ;;
179 esac
180
181 if [[ "${alterlive}" = true ]]; then
182     remove ~/.config/autostart/gensidebar.desktop
183 fi