First versions of the binaries and source file.

This commit is contained in:
Lea 2025-07-04 16:07:49 +02:00
parent d2630d6e4f
commit 1dfda10ddc
11 changed files with 742 additions and 0 deletions

394
daisy.source Executable file
View file

@ -0,0 +1,394 @@
#!/bin/echo "This file can only be sourced, not run stand-alone." -- #!/bin/bash
# LACKADAISICAL SOURCE-ABLE FILE
# Source this in your RC file or manually to receive some of the simpler
# utilities, as well as aliases for `shrc` and `cdf`. Set env variable
# FROM_RC to 1 when sourcing this file to get RC-related functionality:
# FROM_RC=1 source <lackadaisical-root>/daisy.source
``
# This file is also sourced in some of the scripts included within
# lackadaisical for common functionality. Some of the shared functionality is
# only included if sourced from one of the included scripts, though you are
# free to bypass this by setting env variable DAISY_INTERNAL to 1.
[[ $DAISY_INTERNAL -eq 1 ]] && export DAISY_BIN=$(basename $0)
# Intro function
function daisy_help()
{
OLD_IFS="$IFS"
IFS=
echo -e "==================================================================="
echo -e ""
echo -e "Thanks for installing LACKADAISICAL!"
echo -e "This project aims to provide useful utilities as well as learning"
echo -e "material."
echo -e "It is still under heavy development, not all of the things on this"
echo -e "list are present/implemented."
echo -e ""
echo -e "==================================================================="
echo -e ""
echo -e "This suite provides a number of functions, aliases and scripts."
echo -e "They are all aimed at enhancing your efficiency."
echo -e ""
echo -e "==================================================================="
echo -e ""
echo -e "These are the included binaries:"
echo -e " - calm: Reduce a process niceness to 0."
echo -e " - chroot-aio: A chroot wrapper that also takes care of binding"
echo -e " every required directory and has no options."
echo -e " - cdz: This utility extracts an archive to /tmp and changes"
echo -e " directory to it in a new shell instance. Upon exit,"
echo -e " the files are wiped."
echo -e " - editx: Uses your standard CLI editor to create/modify a"
echo -e " file and make it executable."
echo -e " - filewait: This tool is given a filename of a file that does"
echo -e " not exist yet. When the file appears on disk, the"
echo -e " program quits and simply returns the filename. This"
echo -e " can be used in personal workflows."
echo -e " - newday: A basic but powerful journaling system. Recommended"
echo -e " to set up via crontab. Can be used for everything"
echo -e " from diaries to BTRFS snapshots."
echo -e " - own: A simple utility. It effectively uses chown -R"
echo -e " user:user on its target. Root permissions required!"
echo -e " - short: This tool allows you to set up directory shortcuts."
echo -e " It enhances cd t to integrate itsef using its own"
echo -e " syntax. It is similar to wd."
echo -e " - shrc: This tool allows you to edit the RC file for your"
echo -e " shell in your preferred editor. After saving, the"
echo -e " file is sourced by your shell."
echo -e " - sw: A basic function that swaps two files by content."
echo -e " Useful for restoring backups."
echo -e " - what: This is a tool similar to which and others, the key"
echo -e " difference is that it returns partial matches. It can"
echo -e " be used to search for binaries."
echo -e ""
echo -e "==================================================================="
echo -e ""
echo -e "There are aliases and functions included within this file as well:"
echo -e " - bak/unbak: These small utilities make backups of files by making"
echo -e " a copy with a .bak suffix. Unbak reverses the process"
echo -e " using sw and removes the backup."
echo -e " - lsa: A simple alias for ls -lah."
echo -e " - lsn: A simple alias for ls -lah --sort=time --reverse."
echo -e " - editbin: An alias for editx $(which <x>). Saves on typing."
echo -e " - ched: Like chsh but for your editor (EDITOR env). A list"
echo -e " from which you can choose an installed editor"
echo -e " (CLI or GUI) is shown."
echo -e " - cdf: Use fzf to find a file and then cd to its location."
echo -e " - ldrc: Edits this file and source it, similarly to shrc."
echo -e " - daisy_init: Alias for directly sourcing this file from any"
echo -e " LACKADAISICAL binary. You may use this yourself."
echo -e " - daisy_cbin: Contains the name of the current LACKADAISICAL"
echo -e " binary being run."
echo -e " - daisy_enc: Converts a file/stdin to a base64 block that can be"
echo -e " decoded by passing the output(s) to daisy_dec."
echo -e " - *_multi: A version of daisy_enc that runs encodes multiple"
echo -e " files and outputs daisy_base64_data blocks to a file"
echo -e " or stdout."
echo -e " - daisy_dec: Converts daisy_base64_data blocks back to the form"
echo -e " it was in originally."
echo -e " - *_multi: A version of daisy_dec that runs on multiple input"
echo -e " blocks that are either stored in a file or stdin."
echo -e ""
echo -e "==================================================================="
echo -e ""
echo -e "To uninstall LACKADAISICAL, simply remove the source line from your"
echo -e "shell RC, and reload it. This does not remove the files!"
echo -e ""
echo -e "To read this notice again, call the function 'daisy_help'."
echo -e ""
echo -e "==================================================================="
IFS="$OLD_IFS"
}
# Variables for use in other utilities
# Find the right argument for our folder
ARG=$0
if [[ ! $ARG == *daisy.source* ]];
then
ARG="${BASH_SOURCE[0]}"
fi
export DAISY_FOLDER=$(dirname $(realpath $ARG))
export DAISY_SOURCE_FILE=$(realpath $ARG)
export DAISY_AVAILABLE=0
# Config folder setup
export DAISY_CONFIG_FOLDER="$HOME/.config/lackadaisical"
NEW_INSTALL=0
if [[ ! -d "$DAISY_CONFIG_FOLDER" ]];
then
# Create the folder with its basics
mkdir -p "$DAISY_CONFIG_FOLDER"
echo "export EDITOR=$EDITOR" > "$DAISY_CONFIG_FOLDER/editor.src"
daisy_help
NEW_INSTALL=1
fi
# Installation into PATH
if [[ ! $PATH == *"$DAISY_FOLDER"* ]];
then
export PATH="$PATH:$DAISY_FOLDER"
[[ NEW_INSTALL -eq 1 ]] && echo -e "Lackadaisical binaries have been added to your PATH variable."
fi
# Load override from config (default is $EDITOR - so no change is made)
_EDITOR=$(cat "$DAISY_CONFIG_FOLDER"/editor.src | grep "EDITOR=" | sed 's/export EDITOR=//g')
NEED_CHED=0
if [[ -z $EDITOR ]];
then
NEED_CHED=1
fi
# Never call if we're in internal mode
if [[ $DAISY_INTERNAL -eq 1 ]];
then
NEED_CHED=0
fi
# Set up the basic alias for `shrc`
# Do not set these up if DAISY_INTERNAL=1 is set, or infinite recursion could
# occur!
if [[ ! -v DAISY_INTERNAL ]];
then
alias shrc=". shrc"
fi
###############################################################################
# FUNCTIONS and ALIASES #######################################################
###############################################################################
# bak and unbak
function bak()
{
# Input: <file>
target=$1
# Check if file exists
if ! test -f "$target";
then
echo "Path not found: \"$target\""
return 2
fi
# Handle both cases
if [[ UNBAK_MODE -eq 1 ]];
then
cp -R "$target.bak" "$target"
rm -rf "$target.bak"
echo "Restored backup: $target <-- $target.bak"
else
cp -R "$target" "$target.bak"
echo "Backup made: $target --> $target.bak"
fi
}
alias unbak="UNBAK_MODE=1 bak"
alias lsa="ls -lah"
alias lsn="ls -lah --sort=time --reverse"
# Simple version of `cdf`
function cdf()
{
cd $(dirname $(fzf))
}
# for convenience purposes
function editbin()
{
editx $(which $1)
}
# sets a new editor based on commony available ones, and some visual ones
function ched()
{
editors=("nano" "vim" "nvim" "vi" "emacs" "gedit" "kate" "mousepad" "micro" \
"code" "subl" "joe" "kwrite" "gnome-text-editor")
# Find which editors are installed
available_editors=()
for editor in "${editors[@]}";
do
EDITOR_REAL=$(which $editor)
if command -v "$EDITOR_REAL" >/dev/null 2>&1;
then
if [[ $(realpath "$EDITOR") == "$EDITOR_REAL" ]];
then
available_editors+=("$editor (current choice)" "$EDITOR_REAL")
else
available_editors+=("$editor" "$EDITOR_REAL")
fi
fi
done
if [[ ! -z $@ ]];
then
TEXT="$@"
dialog --msgbox "$TEXT" 0 0
fi
# Present all choices
CHOICE=$(dialog --clear --title "Select Text Editor (Recommendation: nano)" \
--menu "Choose one of the installed text editors:" 15 50 6 \
"${available_editors[@]}" 3>&1 1>&2 2>&3)
DIALOG_RET=$?
if [ $DIALOG_RET -ne 0 ];
then
echo "No editor selected."
return
fi
CHOICE=$(which $CHOICE)
echo export EDITOR=$CHOICE > "$DAISY_CONFIG_FOLDER/editor.src"
echo export DAISY_OLD_EDITOR=$EDITOR >> "$DAISY_CONFIG_FOLDER/editor.src"
# Seems silly but this is also where we should export these
source "$DAISY_CONFIG_FOLDER/editor.src"
}
function wait_for_editor()
{
pname="$1"
fname="$2"
# Give some time for a process to launch
sleep 1
while true;
do
ALIVE=$(ps aux | grep $fname | grep $pname)
if [[ $ALIVE == "" ]]
then
break
fi
sleep 1
done
}
function ldrc()
{
FROM_RC=0 $EDITOR "$DAISY_SOURCE_FILE"
wait_for_editor $EDITOR "$DAISY_SOURCE_FILE"
source "$DAISY_SOURCE_FILE"
}
function daisy_enc()
{
has_file=$([[ ! -z $1 ]] && file $1 1>/dev/null; echo $?)
has_file=$([[ has_file -eq 0 ]] && echo 1)
file_info="no data"
file_name="null"
if [[ has_file -eq 1 ]];
then
file_info=$(file $1)
file_name=$(basename $1)
fi
data=$(cat ${1:-/dev/stdin})
base64_inner=$(echo -e "$data" | base64 | tr '\n' '-')
# Print out our block
printf "# File info: $file_info\n"
printf "daisy_data_base64_$file_name=\"$base64_inner\""
}
# Will only take input files, always outputs to stdout
function daisy_enc_multi()
{
[[ ! -d $1 ]] && echo "daisy_dec_multi: No input files specified" && return
for file in "$@"; do
if [[ -f "$file" ]]; then
daisy_enc "$file"
echo # separate blocks with a newline
else
echo "daisy_enc_multi: Skipping non-file: $file"
fi
done
}
function daisy_dec()
{
data=$(cat ${1:-/dev/stdin} | sed -e 's/.*=\"//g' | grep -v "#" | tr -d '\"' | tr -d "'" | tr '-' '\n' )
decoded=$(echo -e "$data" | cut -d "=" -f 1 | base64 --decode)
printf "$decoded"
}
# Will only take a file and directory, sources it to find all encoded data
# Extracts to the directory
function daisy_dec_multi()
{
[[ ! -f $1 ]] && echo "daisy_dec_multi: No input file specified" && return
[[ ! -d $2 ]] && echo "daisy_dec_multi: No output directory specified" && return
declare -a vars=( $(cat $1 | grep -v "# File") )
for enc in "${vars[@]}";
do
file=$(echo -e "$enc" | cut -d "_" -f 4- | cut -d "=" -f 1)
if [[ ! "$file" == '' ]]
then
daisy_dec <(echo "$enc") | tee "$2"/"$file"
fi
done
}
alias daisy_init='source "$DAISY_SOURCE_FILE"'
###############################################################################
# end of FUNCTIONS and ALIASES ################################################
###############################################################################
if [[ $NEED_CHED -eq 1 ]];
then
# Editor is unset, pick one, set `vi` as backup
TXT1="There is no standard EDITOR environment variable defined. Choose one of the installed text editors."
TXT2="You can always change it later wih `ched`, part of the Lackadaisical suite."
ched $TXT1 $TXT2
fi
# End of user section!
export DAISY_AVAILABLE=1
# Start of internal section
# Check for dependencies
function daisy_dependency_check()
{
command -v $1 1>/dev/null 2>/dev/null;
res=$?
echo $(($res ^ 1))
}
DAISY_HAS_fzf=$(daisy_dependency_check fzf)
DAISY_HAS_md5sum=$(daisy_dependency_check md5sum)
function daisy_quit_if_no()
{
has_dep=$DAISY_HAS_$1
# Check first if we have checked for this dependency, if not, print a fixme!
# TODO: Remove upon release, or convert into self-modifying code.
if [[ ! -v DAISY_HAS_$1 ]];
then
echo "FIXME: Dependency `$1` should have an env variable, checking ad-hoc"
has_dep=$(daisy_dependency_check $1)
fi
if [[ $has_dep -eq 0 ]];
then
echo "$DAISY_BIN: The dependency $1 was not found! Please install it" \
"to be able to use this utility!"
exit 1
fi
}
[ -d "$DAISY_FOLDER" ] && export DAISY_AVAILABLE=1
# Source everything in the config folder
# We do this at the end so that we do not run into isues
for file in "$DAISY_CONFIG_FOLDER"/*; do
[ -f "$file" ] && source "$file"
done