OSDN Git Service

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