Skip to content

Instantly share code, notes, and snippets.

@zumikkebe
Last active April 22, 2025 19:09
Show Gist options
  • Save zumikkebe/12c54801bd214293bb87c35061898f11 to your computer and use it in GitHub Desktop.
Save zumikkebe/12c54801bd214293bb87c35061898f11 to your computer and use it in GitHub Desktop.
#! /bin/sh
# This shell script, given a file or directory, attempts to find
# the package it belongs to (if any) and opens that package
# Function to display usage information and exit
Usage ()
{
cat << EOF >&2
usage: $(basename "${0}") <file|dir>
EOF
exit 1
}
# Check if the first argument (a file or directory) is provided
if [ ! -z "${1}" ] ; then
# Store the argument as the target file
pkgd_file="${1}"
else
if [[ ! -t 0 ]] ; then
# Prompt the user with a file selection dialog (filepanel)
# Default directory is the system directory, filtering only files (fds)
pkgd_file=$(filepanel -d $(finddir B_SYSTEM_DIRECTORY) -k fds)
else
# If no argument and input is from a terminal, show usage
Usage
fi
fi
# Check if the selected/provided file/dir actually exists
if [ -e "${pkgd_file}" ] ; then
# Try to retrieve the package attribute from the file
# "catattr -d" reads the attribute "SYS:PACKAGE_FILE" (specific to Haiku)
# pkg_src=$(catattr -d SYS:PACKAGE_FILE "${pkgd_file}" 2>/dev/null)
# I'm using 'findpaths -p ${path_to_file} B_FIND_PATH_PACKAGE_PATH' instead
# hint by Lrrr https://discuss.haiku-os.org/t/haiku-scripting/10094/134
pkg_src=$(findpaths -p "${pkgd_file}" B_FIND_PATH_PACKAGE_PATH 2>/dev/null)
if [[ "${pkg_src}" ]] ; then
# If the attribute exists, open the corresponding package file
open $(finddir B_SYSTEM_PACKAGES_DIRECTORY)/${pkg_src}
else
# If the file does not belong to any package, show an error
cat << EOF >&2
error: ${pkgd_file} does not belong to a package
EOF
exit 1
fi
else
# If the file doesn't exist, show usage
Usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment