1ae96c5eeSMichael Walsh#!/bin/bash
2ae96c5eeSMichael Walsh
3ae96c5eeSMichael Walsh# This script is a wrapper for programs that may have alternate versions
4ae96c5eeSMichael Walsh# (e.g. python2, python3).  This wrapper allows the user to influence the
5ae96c5eeSMichael Walsh# selection of the program version by setting the <program name>_VERSION (e.g.
6ae96c5eeSMichael Walsh# PYTHON_VERSION, ROBOT_VERSION, etc.) environment variable.
7ae96c5eeSMichael Walsh
8ae96c5eeSMichael Walsh# Users would be expected to create a link with the base name of the program
9ae96c5eeSMichael Walsh# that points to this file.
10ae96c5eeSMichael Walsh
11ae96c5eeSMichael Walsh# Example:
12ae96c5eeSMichael Walsh# cd openbmc-test-automation/bin
13ae96c5eeSMichael Walsh# ln -s select_version python
14ae96c5eeSMichael Walsh
15ae96c5eeSMichael Walsh# The PATH variable should contain the expanded path to
16ae96c5eeSMichael Walsh# openbmc-test-automation/bin.
17ae96c5eeSMichael Walsh
18ae96c5eeSMichael Walsh# If <program name>_VERSION is blank or not set, then the program version
19ae96c5eeSMichael Walsh# will be whatever the system default program version is.  If <program
20ae96c5eeSMichael Walsh# name>_VERSION is set to a value, then that value will be appened to the name
21ae96c5eeSMichael Walsh# of the program (e.g. if PYTHON_VERSION = "3", then python3 will be used.).
22ae96c5eeSMichael Walsh# If <program name>_VERSION is set to some value that does not correspond to a
23ae96c5eeSMichael Walsh# valid program version for the given system, this program will fail.
24ae96c5eeSMichael Walsh
25ae96c5eeSMichael Walsh
26ae96c5eeSMichael Walsh# Make sure program_name is set.
27ae96c5eeSMichael Walshprogram_name="${0##*/}"
28ae96c5eeSMichael Walshprogram_uppercase_name=${program_name^^}
29ae96c5eeSMichael Walsh
30ae96c5eeSMichael Walsh
31ae96c5eeSMichael Walshfunction get_target_program_path {
32ae96c5eeSMichael Walsh  local target_program_path_var="${1:-target_program_path}" ; shift
33ae96c5eeSMichael Walsh
34ae96c5eeSMichael Walsh  # Get the full path to the "real" program and assign it to the variable
35ae96c5eeSMichael Walsh  # named in target_program_path_var.
36ae96c5eeSMichael Walsh
37ae96c5eeSMichael Walsh  # Description of argument(s):
38ae96c5eeSMichael Walsh  # target_program_path_var         The name of the variable to receive the
39ae96c5eeSMichael Walsh  #                                 result.
40ae96c5eeSMichael Walsh
41ae96c5eeSMichael Walsh  # Example result:
42ae96c5eeSMichael Walsh
43ae96c5eeSMichael Walsh  # /usr/bin/python3
44ae96c5eeSMichael Walsh
45ae96c5eeSMichael Walsh  local version_var_name
46ae96c5eeSMichael Walsh  local alternate_program_name
47ae96c5eeSMichael Walsh  local base_program_path
48ae96c5eeSMichael Walsh  local base_program_name
49ae96c5eeSMichael Walsh  local candidate
50ae96c5eeSMichael Walsh  local candidates
51ae96c5eeSMichael Walsh  local base_file_path
52ae96c5eeSMichael Walsh
53ae96c5eeSMichael Walsh  # The typical use of this program would be to create a link to it like this:
54ae96c5eeSMichael Walsh  # ln -s select_version python
55ae96c5eeSMichael Walsh  # That being the case, get the name of this actual program (rather than the
56ae96c5eeSMichael Walsh  # name of the link to it).
57ae96c5eeSMichael Walsh  base_program_path=$(readlink -f "${0}")
58ae96c5eeSMichael Walsh  base_program_name=${base_program_path##*/}
59ae96c5eeSMichael Walsh
60*2bcd654fSMichael Walsh  if [ "${program_name}" == "${base_program_name}" ] ; then
61*2bcd654fSMichael Walsh    {
62*2bcd654fSMichael Walsh      echo -n "**ERROR** ${base_program_name} should never be called directly."
63*2bcd654fSMichael Walsh      echo "  Only links to ${base_program_name} should be called."
64*2bcd654fSMichael Walsh    } >&2
65*2bcd654fSMichael Walsh    exit 1
66*2bcd654fSMichael Walsh  fi
67*2bcd654fSMichael Walsh
68ae96c5eeSMichael Walsh  # Compose the version_var_name value (e.g. PYTHON_VERSION).
69ae96c5eeSMichael Walsh  version_var_name=${program_uppercase_name}_VERSION
70ae96c5eeSMichael Walsh  # Compose the alternate_program_name (e.g. python3).
71ae96c5eeSMichael Walsh  alternate_program_name=${program_name}${!version_var_name}
72ae96c5eeSMichael Walsh
73ae96c5eeSMichael Walsh  # Now use the "type" built-in to search the PATH variable for a list of
74ae96c5eeSMichael Walsh  # target program candidates.
75ae96c5eeSMichael Walsh  candidates=$(type -ap ${alternate_program_name})
76ae96c5eeSMichael Walsh
77ae96c5eeSMichael Walsh  # Example candidates:
78ae96c5eeSMichael Walsh  # /home/robot/openbmc-test-automation/bin/python
79ae96c5eeSMichael Walsh  # /usr/bin/python
80ae96c5eeSMichael Walsh
81ae96c5eeSMichael Walsh  # In this example, the first candidate is actually a link to
82ae96c5eeSMichael Walsh  # /home/robot/openbmc-test-automation/bin/select_version.  As such it will
83ae96c5eeSMichael Walsh  # be rejected.
84ae96c5eeSMichael Walsh
85ae96c5eeSMichael Walsh  for candidate in ${candidates}
86ae96c5eeSMichael Walsh  do
87ae96c5eeSMichael Walsh    if [ -L "${candidate}" ] ; then
88ae96c5eeSMichael Walsh      # The candidate is a link so we need to see if it's a link to this
89ae96c5eeSMichael Walsh      # program file.
90ae96c5eeSMichael Walsh      base_file_path=$(readlink "${candidate}")
91ae96c5eeSMichael Walsh      [ "${base_file_path}" == "${base_program_name}" ] && continue
92ae96c5eeSMichael Walsh    fi
93ae96c5eeSMichael Walsh
94ae96c5eeSMichael Walsh    # The candidate is NOT a link so it qualifies as the desired target
95ae96c5eeSMichael Walsh    # program path.
96ae96c5eeSMichael Walsh    eval ${target_program_path_var}=\"\${candidate}\"
97ae96c5eeSMichael Walsh    return
98ae96c5eeSMichael Walsh
99ae96c5eeSMichael Walsh  done
100ae96c5eeSMichael Walsh
101ae96c5eeSMichael Walsh}
102ae96c5eeSMichael Walsh
103ae96c5eeSMichael Walsh
104ae96c5eeSMichael Walsh# Main
105ae96c5eeSMichael Walsh
106ae96c5eeSMichael Walsh  get_target_program_path target_program_path
107ae96c5eeSMichael Walsh
108ae96c5eeSMichael Walsh  # Compose program path var name (e.g. PYTHON_PGM_PATH).
109ae96c5eeSMichael Walsh  pgm_path_var_name=${program_uppercase_name}_PGM_PATH
110ae96c5eeSMichael Walsh  # Set and export pgm_path_var_name (e.g. PYTHON_PGM_PATH=/usr/bin/python3).
111ae96c5eeSMichael Walsh  # This value can be used by child programs for debug.
112ae96c5eeSMichael Walsh  eval export ${pgm_path_var_name}=${target_program_path}
113ae96c5eeSMichael Walsh
114ae96c5eeSMichael Walsh  if [ "${1}" == "--print_only" ] ; then
115ae96c5eeSMichael Walsh    echo "${target_program_path}"
116ae96c5eeSMichael Walsh  else
117a6076076SMichael Walsh    # Use exec in order to replace this process with the target process.
118a6076076SMichael Walsh    exec ${target_program_path} "${@}"
119ae96c5eeSMichael Walsh  fi
120ae96c5eeSMichael Walsh
121