OSDN Git Service

a597d3346d07cac58fa07e343f878520d3e6bb29
[alterlinux/fascode-live-tools.git] / fascode-gtk-bookmarks / fascode-gtk-bookmarks
1 #!/usr/bin/env bash
2 # Yamada Hayao
3 # Twitter: @Hayao0819
4 # Email  : hayao@fascode.net
5 #
6 # kokkiemouse
7 # Mastodon: @kokkiemouse@mstdn.jp 
8 # Email   : kokkiemouse@fascode.net
9 #
10 # (c) 2019-2021 Fascode Network.
11 #
12
13
14 set -e
15
16 force=false
17 fascodelive=false
18 simulation=false
19 bookmark_file="${HOME}/.config/gtk-3.0/bookmarks"
20
21 remove () {
22     local _list
23     local _file
24     _list=($(echo "$@"))
25     for _file in "${_list[@]}"; do
26         if [[ -f ${_file} ]]; then
27             rm -f "${_file}"
28         elif [[ -d ${_file} ]]; then
29             rm -rf "${_file}"
30         fi
31     done
32 }
33
34 _help() {
35     echo "usage ${0} [options] [command]"
36     echo
37     echo " General options:"
38     echo "    -f | --force          Force overwriting"
39     echo "    -s | --simulation     Enable simulation"
40     echo "    -h | --help           This help message and exit"
41     echo
42     echo " General command:"
43     echo "    add <dir>             Add a item to the sidebar"
44     echo "    delete <dir>          Delete item from the sidebar"
45     echo "    alldelete             Delete all sidebar items"
46     echo "    init                  Initializes the sidebar"
47     echo "    help                  This help message and exit"
48 }
49
50 output() {
51     if [[ "${simulation}" = true ]]; then
52         echo "${@}"
53     else
54         echo "${@}" >> "${bookmark_file}"
55     fi
56 }
57
58 _msg_error() {
59     echo "${@}" >&2
60 }
61
62 prepare() {
63     if [[ ! -d "$(dirname "${bookmark_file}")" ]]; then
64         mkdir -p "$(dirname "${bookmark_file}")"
65     fi
66     if [[ ! -f "${bookmark_file}" ]]; then
67         touch "${bookmark_file}"
68     fi
69 }
70
71 add() {
72     if [[ "${simulation}" = false ]]; then
73         prepare
74     fi
75     local dir
76     for dir in ${@}; do
77         if [[ ! -d "${dir}" ]]; then
78             _msg_error "${dir} does not exist."
79             exit 1
80         else
81             output "file://${dir} $(basename "${dir}")"
82         fi
83     done
84 }
85
86
87 delete() {
88     if [[ ! -f "${bookmark_file}" ]]; then
89         _msg_error "Bookmark file does not exist."
90         exit 1
91     fi
92     local _dir _count _line_contain _remove_line=() _url _remove_count i=1
93     for (( i = 1; i <= "${#}"; i++)); do
94         cd "${PWD}"
95         _dir="$(eval echo '$'${i})"
96         _url="$(realpath "${_dir}" | sed "s/ /%20/g")"
97         cd "${OLDPWD}"
98         for _count in $(seq 1 $(cat "${bookmark_file}" | wc -l )); do
99             _line_contain="$(cat "${bookmark_file}" | head -n "${_count}" | tail -n 1 | cut -d ' ' -f 1)"
100             _line_contain="${_line_contain#file://}"
101             if [[ "${_url}" = "${_line_contain}" ]] || [[ "${_url}" = "file://${_line_contain}" ]]; then
102                 _remove_line+=("${_count}")
103             fi
104         done
105         _remove_count=0
106         if (( "${#_remove_line[@]}" == 0 )); then
107             _msg_error "${_dir} is not registered in the sidebar."
108             continue
109         fi
110         for _count in ${_remove_line[@]}; do
111             if [[ "${simulation}" = true ]]; then
112                 sed "${_count}d" "${bookmark_file}"
113             else
114                 _count="$(( _count - _remove_count ))"
115                 sed -i "${_count}d" "${bookmark_file}"
116                 _remove_count="$(( _remove_count + 1 ))"
117             fi
118         done
119     done
120 }
121
122 init() {
123     if [[ "${simulation}" = false ]]; then
124         remove "${bookmark_file}"
125         prepare
126     fi
127
128     source "${HOME}/.config/user-dirs.dirs"
129
130     init_dirs=(
131         "${XDG_DOCUMENTS_DIR}"
132         "${XDG_DOWNLOAD_DIR}"
133         "${XDG_MUSIC_DIR}"
134         "${XDG_PICTURES_DIR}"
135         "${XDG_VIDEOS_DIR}"
136     )
137
138     local dir
139     for dir in "${init_dirs[@]}"; do
140         output "file://${dir} $(basename "${dir}")"
141     done
142 }
143
144
145
146 # Argument analysis and processing
147 options="${@}"
148 _opt_short="fhts"
149 _opt_long="force,help,fascodelive,t-mart,takebayashi,simulation"
150 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
151 if [[ ${?} != 0 ]]; then
152     exit 1
153 fi
154
155 eval set -- "${OPT}"
156 unset OPT
157 unset _opt_short
158 unset _opt_long
159
160
161 while true; do
162     case ${1} in
163         -s | --simulation)
164             simulation=true
165             shift 1
166             ;;
167         -f | --force)
168             force=true
169             shift 1
170             ;;
171         -h | --help)
172             _help
173             shift 1
174             exit 0
175             ;;
176         --fascodelive)
177             fascodelive=true
178             shift 1
179             ;;
180          --t-mart)
181             
182             echo "さすが店長、青春ブタ野郎だね"
183             shift 1
184             exit 0
185             ;;
186          --takebayashi)
187             
188             echo "竹林さん。チノちゃんかわいい最高!!"
189             shift 1
190             exit 0
191             ;;
192          -t)
193             if [[ "$(basename $0)" == "alterlinux-gtk-bookmarks" ]]; then
194                 echo "さすが店長、青春ブタ野郎だね"
195             else
196                 echo "竹林さん。チノちゃんかわいい最高!!"
197             fi
198             shift 1
199             exit 0
200             ;;
201
202         --)
203             shift
204             break
205             ;;
206         *)
207             _msg_error "Invalid argument '${1}'"
208             _help
209             exit 1
210             ;;
211     esac
212 done
213
214 mode="${1}"
215
216 case "${1}" in
217     add) 
218         shift 1
219         if [[ -z "${*}" ]]; then
220             _msg_error "Please specify a directory."
221             exit 1
222         else
223             add "${@}"
224         fi
225         exit 0
226         ;;
227     alldelete)
228         shift 1
229         if [[ "${simulation}" = false ]]; then
230             remove "${bookmark_file}"
231         fi
232         ;;
233     init)
234         shift 1
235         if [[ -f "${bookmark_file}" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]] && [[ -n "$(cat "${bookmark_file}" 2>/dev/null)" ]]; then
236             _msg_error "The sidebar already exists. Use -f to force initialization."
237             exit 1
238         else
239             init
240         fi
241         ;;
242     delete)
243         shift 1
244         if [[ -z "${*}" ]]; then
245             _msg_error "Please specify a directory."
246             exit 1
247         else
248             delete "${@}"
249         fi
250         exit 0
251         ;;
252     help)
253         shift 1
254         _help
255         ;;
256     *)
257         _msg_error "Please specify a command."
258         exit 1
259         ;;
260 esac
261
262 if [[ "${fascodelive}" = true ]]; then
263     remove "${HOME}/.config/autostart/gensidebar.desktop"
264 fi