4 # Email : hayao@fascode.net
7 # Mastodon: @kokkiemouse@mstdn.jp
8 # Email : kokkiemouse@fascode.net
10 # (c) 2019-2021 Fascode Network.
19 bookmark_file="${HOME}/.config/gtk-3.0/bookmarks"
20 backup_dir="${bookmark_file}.d/"
22 # Show message when file is removed
23 # remove <file> <file> ...
29 echo "usage ${0} [options] [command]"
31 echo " General options:"
32 echo " -f | --force Force overwriting"
33 echo " -s | --simulation Enable simulation"
34 echo " -h | --help This help message and exit"
35 echo " --bakdir [dir] Specify the directory used for backup and restore"
37 echo " General command:"
38 echo " add <dir> <name> ... Add a item to the sidebar"
39 echo " delete <dir> ... Delete item from the sidebar"
40 echo " alldelete Delete all sidebar items"
41 echo " init Initializes the sidebar"
42 echo " backup Backup sidebar"
43 echo " restore Restore bookmark from backup"
44 echo " help This help message and exit"
48 # Returns only the selected result to standard output
49 # _ask -d <デフォルト値> -p <質問文> <選択肢1> <選択肢2> ...
51 local arg OPTARG OPTIND _default="" _choice_list _count _choice _question
52 while getopts "d:p:" arg; do
54 d) _default="${OPTARG}" ;;
55 p) _question="${OPTARG}" ;;
59 shift "$((OPTIND - 1))"
64 if (( ${#_choice_list[@]} < 0 )); then
65 _msg_error "An exception error has occurred."
69 # 選択肢が1つしか無いならばそのまま値を返す
70 if (( ${#_choice_list[@]} <= 1 )); then
71 echo "${_choice_list[*]}"
75 if [[ -v _question ]] && [[ ! "${_question}" = "" ]]; then
76 echo -e "${_question}" >&2
79 for (( _count=1; _count<=${#_choice_list[@]}; _count++)); do
80 _choice="${_choice_list[$(( _count - 1 ))]}"
81 if [[ ! "${_default}" = "" ]] && [[ "${_choice}" = "${_default}" ]]; then
82 printf " * %${_digit}d: ${_choice}\n" "${_count}" >&2
84 printf " %${_digit}d: ${_choice}\n" "${_count}" >&2
88 echo -n "(1 ~ ${#_choice_list[@]}) > " >&2
92 if printf "%s" "${_input}" | grep -E "^[0-9]+$" 1>/dev/null 2>&1; then
94 if (( 1 <= _input)) && (( _input <= ${#_choice_list[@]} )); then
95 _choice="${_choice_list[$(( _input - 1 ))]}"
101 if printf "%s\n" "${_choice_list[@]}" | grep -x "${_input}" 1>/dev/null 2>&1; then
112 if [[ "${simulation}" = true ]]; then
115 echo "${@}" >> "${bookmark_file}"
124 if [[ ! -d "$(dirname "${bookmark_file}")" ]]; then
125 mkdir -p "$(dirname "${bookmark_file}")"
127 if [[ ! -f "${bookmark_file}" ]]; then
128 touch "${bookmark_file}"
134 if [[ "${simulation}" = false ]]; then
139 if [[ -z "${1+SET}" ]]; then
142 if [[ -d "${1}" ]]; then
145 if [[ -n "${1+SET}" ]] && [[ ! -d "${1}" ]]; then
149 name="$(basename "${dir}")"
152 _msg_error "${dir} does not exist."
155 echo "Added bookmark ${dir} as ${name}"
156 output "file://${dir} ${name}"
163 if [[ ! -f "${bookmark_file}" ]]; then
164 _msg_error "Bookmark file does not exist."
167 local _dir _count _line_contain _remove_line=() _url _remove_count i=1
168 for (( i = 1; i <= "${#}"; i++)); do
170 _dir="$(eval echo '$'${i})"
171 _url="$(realpath "${_dir}" | sed "s/ /%20/g")"
173 for _count in $(seq 1 $(cat "${bookmark_file}" | wc -l )); do
174 _line_contain="$(cat "${bookmark_file}" | head -n "${_count}" | tail -n 1 | cut -d ' ' -f 1)"
175 _line_contain="${_line_contain#file://}"
176 if [[ "${_url}" = "${_line_contain}" ]] || [[ "${_url}" = "file://${_line_contain}" ]] || [[ "${_url}/" = "${_line_contain}" ]] || [[ "${_url}/" = "file://${_line_contain}" ]]; then
177 _remove_line+=("${_count}")
181 if (( "${#_remove_line[@]}" == 0 )); then
182 _msg_error "${_dir} is not registered in the sidebar."
185 for _count in "${_remove_line[@]}"; do
186 if [[ "${simulation}" = true ]]; then
187 sed "${_count}d" "${bookmark_file}"
189 _count="$(( _count - _remove_count ))"
190 sed -i "${_count}d" "${bookmark_file}"
191 _remove_count="$(( _remove_count + 1 ))"
198 if [[ "${simulation}" = false ]]; then
199 remove "${bookmark_file}"
203 source "${HOME}/.config/user-dirs.dirs"
206 "${XDG_DOCUMENTS_DIR}"
207 "${XDG_DOWNLOAD_DIR}"
209 "${XDG_PICTURES_DIR}"
214 for dir in "${init_dirs[@]}"; do
215 output "file://${dir} $(basename "${dir}")"
220 if [[ ! -f "${bookmark_file}" ]]; then
221 echo "Initialize with the init command or add the directory with the add command."
224 mkdir -p "${backup_dir}"
225 local path="${backup_dir}/$(date +%s).bak"
226 cp "${bookmark_file}" "${path}"
227 echo "Backuped to ${path}"
231 local backups=() target_date target_path
232 readarray -t backups < <(find "${backup_dir}" -mindepth 1 -maxdepth 1 -name "*.bak" -type f -printf "%f\0" | xargs -0 -I{} bash -c 'echo {} | sed "s|.bak$||g"' | xargs -I{} date --date "@{}" "+%Y/%m/%d %T")
233 if (( "${#backups[@]}" == 0 )); then
234 _msg_error "Backup was not found"
237 if [[ -f "${bookmark_file}" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]] && [[ -n "$(cat "${bookmark_file}" 2>/dev/null)" ]]; then
238 _msg_error "The sidebar already exists. Use -f to force restore."
241 if ! target_date=$(_ask -p "Select the backup you want to use" "${backups[@]}"); then
244 target_path="${backup_dir}/$(date -d "${target_date}" +%s).bak"
245 if [[ -f "${target_path}" ]]; then
246 remove "${bookmark_file}"
247 cp "${target_path}" "${bookmark_file}"
249 _msg_error "A backup that does not exist was selected."
256 # Argument analysis and processing
259 OPTL=("force" "help" "fascodelive" "t-mart" "takebayashi" "simulation" "alterlive" "bakdir:")
260 if ! OPT=$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" -- "${ARGUMENT[@]}"); then
265 unset ARGUMENT OPTS OPTL OPT
283 --fascodelive | --alterlive)
288 echo "さすが店長、青春ブタ野郎だね"
293 echo "竹林さん。チノちゃんかわいい最高!!"
298 if [[ "$(basename $0)" == "alterlinux-gtk-bookmarks" ]]; then
299 echo "さすが店長、青春ブタ野郎だね"
301 echo "竹林さん。チノちゃんかわいい最高!!"
315 _msg_error "Invalid argument '${1}'"
327 if [[ -z "${*}" ]]; then
328 _msg_error "Please specify a directory."
337 if [[ "${simulation}" = false ]]; then
338 remove "${bookmark_file}"
343 if [[ -f "${bookmark_file}" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]] && [[ -n "$(cat "${bookmark_file}" 2>/dev/null)" ]]; then
344 _msg_error "The sidebar already exists. Use -f to force initialization."
352 if [[ -z "${*}" ]]; then
353 _msg_error "Please specify a directory."
373 _msg_error "Please specify a command."
378 if [[ "${fascodelive}" = true ]]; then
379 remove "${HOME}/.config/autostart/gensidebar.desktop"