blob: 148fe4b07563da5c38fe0e23117620458a5d0793 [file] [log] [blame]
Michael Walsh2c3d6ed2019-05-29 17:13:01 -05001#!/bin/bash
2
3# Universal bash program setup functions.
4
5# Example usage:
6
7# Source files to get required functions.
8# source_files="gen_setup.sh"
9# source_file_paths=$(type -p ${source_files})
10# for file_path in ${source_file_paths} ; do source ${file_path} ; done
11
12# Turn on extended globbing.
13shopt -s extglob
14
15
Patrick Williams90dfee32022-12-08 06:52:46 -060016function get_pgm_path_info() {
17 local program_path_var="${1:-program_path}" ; shift
18 local program_name_var="${1:-program_name}" ; shift
19 local program_dir_path_var="${1:-program_dir_path}" ; shift
20 local follow_links="${1:-0}" ; shift
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050021
Patrick Williams90dfee32022-12-08 06:52:46 -060022 # Determine the program path, name and dir path and assign them to the variables indicated by the caller.
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050023
Patrick Williams90dfee32022-12-08 06:52:46 -060024 # Description of argument(s):
25 # program_path_var The name of the variable to receive the program path.
26 # program_name_var The name of the variable to receive the program name.
27 # program_dir_path_var The name of the variable to receive the program dir path.
28 # follow_links If the program running is actually a link to another file, use that file
29 # when calculating the above values.
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050030
Patrick Williams90dfee32022-12-08 06:52:46 -060031 local _spn_loc_program_path_="${0}"
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050032
Patrick Williams90dfee32022-12-08 06:52:46 -060033 # The program name is the program path minus all characters up to and including the first slash.
34 local _spn_loc_program_name_=${_spn_loc_program_path_##*/}
35 # The program dir path is the program path minus everything from the last slash to the end of the string.
36 local _spn_loc_program_dir_path_=${_spn_loc_program_path_%${_spn_loc_program_name_}}
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050037
Patrick Williams90dfee32022-12-08 06:52:46 -060038 # If program dir path does not start with a slash then it is relative. Convert it to absolute.
39 if [ "${_spn_loc_program_dir_path_:0:1}" != "/" ] ; then
40 _spn_loc_program_dir_path_="$(readlink -f ${_spn_loc_program_dir_path_})/"
41 # Re-assemble the parts into program path variable.
42 _spn_loc_program_path_="${_spn_loc_program_dir_path_}${_spn_loc_program_name_}"
43 fi
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050044
Patrick Williams90dfee32022-12-08 06:52:46 -060045 if (( follow_links )) ; then
46 _spn_loc_program_path_=$(readlink -f ${_spn_loc_program_path_})
47 # Re-calculate program_name in case it is different now.
48 _spn_loc_program_name_=${_spn_loc_program_path_##*/}
49 fi
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050050
Patrick Williams90dfee32022-12-08 06:52:46 -060051 # Set caller's variables.
52 cmd_buf="${program_path_var}=\"\${_spn_loc_program_path_}\" ; ${program_name_var}=\"\${_spn_loc_program_name_}\" ; ${program_dir_path_var}=\"\${_spn_loc_program_dir_path_}\""
53 eval "${cmd_buf}"
Michael Walsh2c3d6ed2019-05-29 17:13:01 -050054
55}