OSDN Git Service

[update] : Use dirname for label
[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     if [[ "${simulation}" = false ]]; then
67         prepare
68     fi
69     local dir
70     for dir in ${@}; do
71         if [[ ! -d "${dir}" ]]; then
72             _msg_error "${dir} does not exist."
73             exit 1
74         else
75             output "file://${dir}"
76         fi
77     done
78 }
79
80 init() {
81     if [[ "${simulation}" = false ]]; then
82         remove "${HOME}/.config/gtk-3.0/bookmarks"
83         prepare
84     fi
85
86     source "${HOME}/.config/user-dirs.dirs"
87
88     init_dirs=(
89         "${XDG_DOCUMENTS_DIR}"
90         "${XDG_DOWNLOAD_DIR}"
91         "${XDG_MUSIC_DIR}"
92         "${XDG_PICTURES_DIR}"
93         "${XDG_VIDEOS_DIR}"
94     )
95
96     local dir
97     for dir in "${init_dirs[@]}"; do
98         output "file://${dir} $(basename "${dir}")"
99     done
100 }
101
102
103
104 # Argument analysis and processing
105 options="${@}"
106 _opt_short="fhts"
107 _opt_long="force,help,alterlive,t-mart,simulation"
108 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
109 if [[ ${?} != 0 ]]; then
110     exit 1
111 fi
112
113 eval set -- "${OPT}"
114 unset OPT
115 unset _opt_short
116 unset _opt_long
117
118
119 while true; do
120     case ${1} in
121         -s | --simulation)
122             simulation=true
123             shift 1
124             ;;
125         -f | --force)
126             force=true
127             shift 1
128             ;;
129         -h | --help)
130             _help
131             shift 1
132             exit 0
133             ;;
134         --alterlive)
135             alterlive=true
136             shift 1
137             ;;
138         -t | --t-mart)
139             echo "さすが店長、青春ブタ野郎だね"
140             shift 1
141             exit 0
142             ;;
143         --)
144             shift
145             break
146             ;;
147         *)
148             _msg_error "Invalid argument '${1}'"
149             _help
150             exit 1
151             ;;
152     esac
153 done
154
155 mode="${1}"
156
157 case "${1}" in
158     add) 
159         shift 1
160         if [[ -z "${*}" ]]; then
161             _msg_error "Please specify a directory."
162             exit 1
163         else
164             add "${@}"
165         fi
166         exit 0
167         ;;
168     delete)
169         if [[ "${simulation}" = false ]]; then
170             remove "${HOME}/.config/gtk-3.0/bookmarks"
171         fi
172         exit 0
173         ;;
174     init)
175         if [[ -f "${HOME}/.config/gtk-3.0/bookmarks" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]]; then
176             _msg_error "The sidebar already exists. Use -f to force initialization."
177             exit 1
178         else
179             init
180         fi
181         exit 0
182         ;;
183     help)
184         _help
185         exit 0
186         ;;
187     *)
188         _msg_error "Please specify a command."
189         exit 1
190         ;;
191 esac
192
193 if [[ "${alterlive}" = true ]]; then
194     remove ~/.config/autostart/gensidebar.desktop
195 fi