File: //usr/syno/bin/synogear
#!/bin/bash
PROGRAM_NAME="$(basename "$0")"
NORMAL_INSTALL_PATH="/var/packages/DiagnosisTool"
NORMAL_TOOL_PATH="/var/packages/DiagnosisTool/target/tool"
ENGINEER_INSTALL_PATH="/var/packages/DiagnosisTool-Eng"
ENGINEER_TOOL_PATH="/var/packages/DiagnosisTool-Eng/target/tool"
NORMAL_TEMP_PROFILE_DIR="/var/packages/DiagnosisTool/etc"
if [[ "${DEBUG_MODE}" = "yes" ]]; then
set -x
fi
Usage() #{{{
{
cat << EOF
usage: ${PROGRAM_NAME} <command>
The most commonly used commands are:
install Get and run this package, also load all tools
remove Remove it
help Show this usage
list List all tools
check Check if tools are ready to use
Examples:
1. ${PROGRAM_NAME} install
Install debug tools and load them into environment
2. ${PROGRAM_NAME} remove
Remove tools and restore all settings
3. ${PROGRAM_NAME} list
List all loaded tools
EOF
} #}}}
Is_Normal_Installed()
{
if [[ -d "$NORMAL_INSTALL_PATH" ]]; then
return 0
else
return 1
fi
}
Is_Engineer_Installed()
{
if [[ -d "$ENGINEER_INSTALL_PATH" ]]; then
return 0
else
return 1
fi
}
Is_Normal_Loaded()
{
if [[ ${PATH} == *"${NORMAL_TOOL_PATH}"* ]]; then
return 0
else
return 1
fi
}
Is_Engineer_Loaded()
{
if [[ ${PATH} == *"${ENGINEER_TOOL_PATH}"* ]]; then
return 0
else
return 1
fi
}
ShowCurrentStateMessage()
{
if (! Is_Normal_Installed) && Is_Normal_Loaded; then
echo "DiagnosisTools are removed completely, you can issue exit to close this session."
elif ! Is_Normal_Installed; then
echo "Tools are not installed yet. You can run this command to install it:"
echo " ${PROGRAM_NAME} install"
elif Is_Normal_Loaded; then
echo "Tools are installed and ready to use."
if [[ -d "$NORMAL_TOOL_PATH" ]]; then
echo "DiagnosisTool version: $(synopkg version DiagnosisTool)"
fi
if [[ -d "$ENGINEER_TOOL_PATH" ]]; then
echo "DiagnosisTool-Eng version: $(synopkg version DiagnosisTool-Eng)"
fi
else
echo "Tools are installed but not loaded yet. You can run this command to load it:"
echo " ${PROGRAM_NAME} install"
fi
}
TryLoadTools()
{
local tool_path=""
if Is_Normal_Installed && (! Is_Normal_Loaded); then
tool_path="$NORMAL_TOOL_PATH:$tool_path"
fi
if Is_Engineer_Installed && (! Is_Engineer_Loaded); then
tool_path="$ENGINEER_TOOL_PATH:$tool_path"
fi
if [[ -z "$tool_path" ]]; then
return 0
fi
# shellcheck disable=2016
local -r ps1_expression='\[\e[91m\]${SYNOGEAR_PROMPT_MARK} \[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]\$ '
if [[ ! -d "${NORMAL_TEMP_PROFILE_DIR}" ]]; then
echo "no temp profile folder found! exit. dir path: ${NORMAL_TEMP_PROFILE_DIR}"
return 1
fi
grep -v \
-e '^PATH=' \
-e '^export PATH' \
'/etc/profile' > "${NORMAL_TEMP_PROFILE_DIR}/.profile"
{
printf '\n'
printf 'export PATH=%s:%s\n' "${tool_path}" "${PATH}"
printf 'export SYNOGEAR_PROMPT_MARK="(synogear)"\n'
printf 'PS1=%s%s%s\n' "'" "${ps1_expression}" "'"
} >> "${NORMAL_TEMP_PROFILE_DIR}/.profile"
# use /bin/sh, unless we have to use ash ($SHELL)
ENV="${NORMAL_TEMP_PROFILE_DIR}/.profile" /bin/sh
}
Action_Load()
{
if TryLoadTools; then
ShowCurrentStateMessage
return 0
else
ShowCurrentStateMessage
return 1
fi
}
DownloadPackage()
{
local -r package_id="$1"
local -r store_path="${2?"store path must be specified"}"
local -r version_file="/etc.defaults/VERSION"
local -r synoinfo_file="/etc.defaults/synoinfo.conf"
local -r lang="enu"
local major_version="$(synogetkeyvalue "${version_file}" "majorversion")"
local minor_version="$(synogetkeyvalue "${version_file}" "minorversion")"
local build_version="$(synogetkeyvalue "${version_file}" "buildnumber")"
local time_zone="$(synogetkeyvalue "${synoinfo_file}" "timezone")"
local unique="$(synogetkeyvalue "${synoinfo_file}" "unique")"
local pkgupdate_url="$(synogetkeyvalue "${synoinfo_file}" "pkgupdate_server")"
local reference_json=""
local download_url=""
local pkg_version=""
pkgupdate_url="${pkgupdate_url:-"https://pkgupdate.synology.com"}/firmware/v1/get"
pkgupdate_url="${pkgupdate_url}?language=${lang}"
pkgupdate_url="${pkgupdate_url}&timezone=${time_zone}"
pkgupdate_url="${pkgupdate_url}&unique=${unique}"
pkgupdate_url="${pkgupdate_url}&major=${major_version}"
pkgupdate_url="${pkgupdate_url}&minor=${minor_version}"
pkgupdate_url="${pkgupdate_url}&build=${build_version}"
pkgupdate_url="${pkgupdate_url}&package_update_channel=stable"
pkgupdate_url="${pkgupdate_url}&package=${package_id}"
if ! reference_json="$(curl -s -L "${pkgupdate_url}")"; then
echo "can't get correct package link string!"
return 1
fi
if ! download_url="$(echo "${reference_json}" | jq -e -r '.["package"]["link"]')"; then
echo "failed to get ${package_id} ... can't parse actual package download link from info file"
return 1
fi
pkg_version="$(echo "${reference_json}" | jq -e -r '.["package"]["version"]')"
if ! curl -s -L "${download_url}" -o "${store_path}"; then
echo "failed to get ${package_id} ${pkg_version} from synology server!"
return 1
fi
echo "download ${package_id} ${pkg_version} successfully"
return 0
}
InstallTool()
{
local -r package_id="$1"
local -r package_spk_temp_path="/tmp/${package_id}.spk"
rm -f "${package_spk_temp_path}"
if ! DownloadPackage "${package_id}" "${package_spk_temp_path}"; then
return 1
fi
local -r install_output=$(synopkg install "${package_spk_temp_path}")
local -r install_exitcode=$?
rm -f "${package_spk_temp_path}"
if [[ $install_exitcode -ne 0 ]]; then
echo "Failed to install ${package_id} ... synopkg error code: ${install_exitcode}"
echo $install_output
return 1
else
return 0
fi
}
StartTool()
{
local -r package_id="$1"
local -r start_output=$(synopkg start "${package_id}")
local -r start_exitcode=$?
if [[ $start_exitcode -ne 0 ]]; then
echo "Failed to start ${package_id} ... synopkg error code: ${start_exitcode}"
echo $start_output
return 1
else
return 0
fi
}
TryInstallAndStartTools()
{
if ! Is_Normal_Installed; then
InstallTool "DiagnosisTool" || return 1
fi
if Is_Normal_Installed; then
StartTool "DiagnosisTool" || return 1
fi
if ! Is_Engineer_Installed; then
InstallTool "DiagnosisTool-Eng" > /dev/null
fi
if Is_Engineer_Installed; then
StartTool "DiagnosisTool-Eng"
fi
return 0
}
Action_Install()
{
local need_install=1
if (! Is_Normal_Installed) || (! Is_Normal_Loaded); then
need_install=0
fi
if (! Is_Engineer_Installed) || (! Is_Engineer_Loaded); then
need_install=0
fi
if [[ $need_install -eq 0 ]]; then
(TryInstallAndStartTools && TryLoadTools) || return 1
fi
ShowCurrentStateMessage
return 0
}
Action_Remove()
{
if Is_Engineer_Installed; then
if ! synopkg uninstall DiagnosisTool-Eng > /dev/null; then
echo "failed to remove DiagnosisTool-Eng package ..."
return 1
fi
fi
if Is_Normal_Installed; then
if ! synopkg uninstall DiagnosisTool > /dev/null; then
echo "failed to remove DiagnosisTool package ..."
return 1
fi
fi
ShowCurrentStateMessage
return 0
}
Action_Check()
{
ShowCurrentStateMessage
}
Action_List()
{
local all_tools=""
if Is_Normal_Loaded; then
all_tools="$(ls "${NORMAL_TOOL_PATH}/") ${all_tools}"
fi
if Is_Engineer_Loaded; then
all_tools="$(ls "${ENGINEER_TOOL_PATH}/") ${all_tools}"
fi
if [[ -n "$all_tools" ]]; then
echo "All tools:"
echo $all_tools
else
ShowCurrentStateMessage
fi
}
case $1 in
install)
Action_Install
;;
remove)
Action_Remove
;;
load)
Action_Load
;;
check)
Action_Check
;;
list)
Action_List
;;
help)
Usage
;;
*)
Action_Check
Usage
esac
# vim: ts=4 sts=4 sw=4 noet: