+#!/bin/sh
+# $TheSupernovaDuo: kanako,v 2.1 2023/10/25 01:50:28 yakumo_izuru Exp $
+
+readonly kanako_conf_dir="${kanako_conf_dir:-$HOME/.config/kanako}"
+readonly kanako_key_dir="${kanako_key_dir:-$HOME/.kanako}"
+readonly kanako_store_dir="${kanako_store_dir:-$HOME/.kanako-store}"
+
+if test -d "${kanako_store_dir}"; then
+ cd "${kanako_store_dir}"
+else
+ printf "Password store not found!\n"
+ printf "Create it with mkdir -p %s\n" "$kanako_store_dir"
+ exit 1
+fi
+
+if test -f "${kanako_conf_dir}/kanako.conf"; then
+ . "${kanako_conf_dir}/kanako.conf"
+else
+ printf "Configuration file has not been found!\n"
+ printf "Copy kanako.conf from the examples directory,\n"
+ printf "and edit accordingly.\n"
+ exit 1
+fi
+
+copy() {
+ view "$1" | "${kanako_clip_cmd}"
+}
+edit() {
+ if [ -f ${1%%.enc}.enc ]; then
+ ${kanako_encrypt_cmd} ${kanako_decrypt_args} ${1%%.enc}.enc >${1%%.enc}
+ "${EDITOR:-${EDITOR:-vi}}" "${1%%.enc}"
+ ${kanako_encrypt_cmd} ${kanako_encrypt_args} ${1%%.enc} >${1%%.enc}.enc
+ rm "${1%%.enc}"
+ else
+ printf "%s does not exist, maybe there was a typo on your end?\n" "${1%%.enc}.enc"
+ fi
+}
+gen() {
+ printf "%s\n" $(strings </dev/urandom | dd bs=1 count="${1:-30}" 2>/dev/null | tr -d ' \t\n\r')
+}
+list() {
+ if [ -z "$1" ]; then
+ $(which tree) "${kanako_store_dir}/" | sed 's/\.enc//g'
+ else
+ $(which tree) "${1:-.}" | sed 's/\.enc//g'
+ fi
+}
+new() {
+ test -d "$1" && usage
+
+ tmpfile="$(mktemp)"
+ "${EDITOR:-${EDITOR:-vi}}" "${tmpfile}"
+
+ mkdir -p "$(dirname "$1")"
+ ${kanako_encrypt_cmd} ${kanako_encrypt_args} ${tmpfile} >${1%%.enc}.enc
+ rm ${tmpfile}
+}
+delete_directory() {
+ rm -r -I "$1"
+}
+delete_file() {
+ rm -i "${1}${2}".enc
+}
+usage() {
+ printf "Usage: %s [-c|-e|-g|-l|-n|-R|-r|-v [file or directory]]\n" "$0"
+ printf "The arguments for all switches are relative to \${kanako_store_dir}\n"
+ printf "which is located at %s\n" "${kanako_store_dir}"
+ exit 1
+}
+view() {
+ if [ -f "${1%%.enc}".enc ]; then
+ ${kanako_encrypt_cmd} ${kanako_decrypt_args} "${1%%.enc}".enc
+ elif [ -d "${1:-.}" ]; then
+ list "${1:-.}"
+ else
+ usage
+ fi
+}
+
+case $1 in
+-c) copy $2 ;;
+-e) edit $2 ;;
+-g) gen $2 ;;
+-l) list $2 ;;
+-n) new $2 ;;
+-R) delete_directory $2 ;;
+-r) delete_file $2 ;;
+-v) view $2 ;;
+*) usage ;;
+esac