#!/usr/bin/env bash
# Yamada Hayao
# Twitter: @Hayao0819
# Email : hayao@fascode.net
#
# (c) 2019-2020 Fascode Network.
#
set -e
force=false
alterlive=false
simulation=false
remove () {
local _list
local _file
_list=($(echo "$@"))
for _file in "${_list[@]}"; do
if [[ -f ${_file} ]]; then
rm -f "${_file}"
elif [[ -d ${_file} ]]; then
rm -rf "${_file}"
fi
done
}
_help() {
echo "usage ${0} [options] [command]"
echo
echo " General options:"
echo " -f | --force Force overwriting"
echo " -s | --simulation Enable sumulation"
echo " -h | --help This help message and exit"
echo
echo " General command:"
echo " add
Add items to the sidebar"
echo " delete Delete all sidebar items"
echo " init Initializes the sidebar"
echo " help This help message and exit"
}
output() {
if [[ "${simulation}" = true ]]; then
echo "${@}"
else
echo "${@}" >> "${HOME}/.config/gtk-3.0/bookmarks"
fi
}
_msg_error() {
echo "${@}" >&2
}
prepare() {
if [[ ! -d "${HOME}/.config/gtk-3.0/" ]]; then
mkdir -p "${HOME}/.config/gtk-3.0/"
fi
if [[ ! -f "${HOME}/.config/gtk-3.0/bookmarks" ]]; then
touch "${HOME}/.config/gtk-3.0/bookmarks"
fi
}
add() {
if [[ "${simulation}" = false ]]; then
prepare
fi
local dir
for dir in ${@}; do
if [[ ! -d "${dir}" ]]; then
_msg_error "${dir} does not exist."
exit 1
else
output "file://${dir}"
fi
done
}
init() {
if [[ "${simulation}" = false ]]; then
remove "${HOME}/.config/gtk-3.0/bookmarks"
prepare
fi
source "${HOME}/.config/user-dirs.dirs"
init_dirs=(
"${XDG_DOCUMENTS_DIR}"
"${XDG_DOWNLOAD_DIR}"
"${XDG_MUSIC_DIR}"
"${XDG_PICTURES_DIR}"
"${XDG_VIDEOS_DIR}"
)
local dir
for dir in "${init_dirs[@]}"; do
output "file://${dir} $(basename "${dir}")"
done
}
# Argument analysis and processing
options="${@}"
_opt_short="fhts"
_opt_long="force,help,alterlive,t-mart,simulation"
OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
if [[ ${?} != 0 ]]; then
exit 1
fi
eval set -- "${OPT}"
unset OPT
unset _opt_short
unset _opt_long
while true; do
case ${1} in
-s | --simulation)
simulation=true
shift 1
;;
-f | --force)
force=true
shift 1
;;
-h | --help)
_help
shift 1
exit 0
;;
--alterlive)
alterlive=true
shift 1
;;
-t | --t-mart)
echo "さすが店長、青春ブタ野郎だね"
shift 1
exit 0
;;
--)
shift
break
;;
*)
_msg_error "Invalid argument '${1}'"
_help
exit 1
;;
esac
done
mode="${1}"
case "${1}" in
add)
shift 1
if [[ -z "${*}" ]]; then
_msg_error "Please specify a directory."
exit 1
else
add "${@}"
fi
exit 0
;;
delete)
if [[ "${simulation}" = false ]]; then
remove "${HOME}/.config/gtk-3.0/bookmarks"
fi
exit 0
;;
init)
if [[ -f "${HOME}/.config/gtk-3.0/bookmarks" ]] && [[ "${force}" = false ]] && [[ "${simulation}" = false ]]; then
_msg_error "The sidebar already exists. Use -f to force initialization."
exit 1
else
init
fi
exit 0
;;
help)
_help
exit 0
;;
*)
_msg_error "Please specify a command."
exit 1
;;
esac
if [[ "${alterlive}" = true ]]; then
remove ~/.config/autostart/gensidebar.desktop
fi