#!/bin/bash
#
#	Put in one place messages for installing missing tools/components.
#	The packages / names often differ from system to system
#

TOOL=$1

OS=$OSTYPE
DistroBasedOn="Unknown"

if [ -f /etc/redhat-release ] ; then
    DistroBasedOn='RedHat'
    DIST=`cat /etc/redhat-release |sed s/\ release.*//`
    PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
    REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SuSE-release ] ; then
    DistroBasedOn='SuSe'
    PSUEDONAME=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
    REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
    DistroBasedOn='Mandrake'
    PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
    REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/debian_version ] ; then
    DistroBasedOn='Debian'
    DIST=`cat /etc/lsb-release | grep '^DISTRIB_ID' | awk -F=  '{ print $2 }'`
    PSUEDONAME=`cat /etc/lsb-release | grep '^DISTRIB_CODENAME' | awk -F=  '{ print $2 }'`
    REV=`cat /etc/lsb-release | grep '^DISTRIB_RELEASE' | awk -F=  '{ print $2 }'`
elif [ `uname -s` == "Darwin"  ] ; then
    DistroBasedOn='Darwin'
    PSUEDONAME="Darwin - MacOSX"
    REV=""
fi
case `uname` in
   CYGWIN*)  DistroBasedOn='cygwin' ;;
   MSYS*)  DistroBasedOn='msys' ;;
esac

echo -n "Missing component $TOOL"

# First Tool specific hints (overrides)
if [ $TOOL == "libtool" ] ; then
	if [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y libtool-bin" && echo && exit 0
	fi
elif [ $TOOL == "7za" ] ; then
	if [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y p7zip-full" && echo && exit 0
	elif [ $DistroBasedOn == "cygwin" ] ; then
		echo -n ": try cygwin setup, package name p7zip" && echo && exit 0
	elif [ $DistroBasedOn == "msys" ] ; then
		echo -n ": try pacman -S p7zip" && echo && exit 0
	elif [ $DistroBasedOn == "Darwin" ] ; then
		echo -n ": try brew install p7zip" && echo && exit 0
	fi
elif [ $TOOL == "tr" ] ; then
	if [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y coreutils" && echo && exit 0
	fi
elif [ $TOOL == "clang-format" ] ; then
	if [ $DistroBasedOn == "msys" ] ; then
	echo -n ": export PATH=\"\$PATH:/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/bin\" to ~/.bashrc" 
	echo -n "or try pacman -S clang" && echo && exit 0
	elif [ $DistroBasedOn == "cygwin" ] ; then
	echo -n ": export PATH=\"\$PATH:/cygdrive/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/bin\" to ~/.bashrc" 
	echo -n "or try cygwin setup clang" && echo && exit 0
	fi
elif [ $TOOL == "unix2dos" ] ; then
	if [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y dos2unix" && echo && exit 0
	elif [ $DistroBasedOn == "cygwin" ] ; then
		echo -n ": try cygwin setup, package name dos2unix" && echo && exit 0
	fi
elif [ $TOOL == "realpath" ] ; then
	if [ $DistroBasedOn == "Darwin" ] ; then
		echo -n ": try make install-realpath" && echo && exit 0
	elif [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y coreutils" && echo && exit 0
	fi
elif [ $TOOL == "node" ] ; then
	if [ $DistroBasedOn == "cygwin" ] ; then
		echo -n ": try installing https://nodejs.org/en/ msi installer" && echo && exit 0
	elif [ $DistroBasedOn == "Debian" ] ; then
		echo -n ": try apt-get install -y nodejs" && echo && exit 0
	fi
elif [ $TOOL == "quasar" ] ; then
	echo -n ": (maybe first install nodejs/npm) then npm i -g npm @quasar/cli" && echo && exit 0
elif [ $TOOL == "sed" ] ||  [ $TOOL == "gsed" ] ; then
	if [ $DistroBasedOn == "Darwin" ] ; then
		echo -n ": try brew install gnu-sed" && echo && exit 0
	fi
elif [ $TOOL == "jq" ] ; then
	if [ $DistroBasedOn == "msys" ] ; then
	echo -n ": try pacman -S mingw-w64-x86_64-jq; and maybe add export PATH=\"\$PATH:/mingw64/bin\" to ~/.bashrc" && echo && exit 0
	fi
fi

# Generic instructions based on your 'OS'/build environment
if [ $DistroBasedOn == "Darwin" ] ; then
	echo -n ": try brew install $TOOL" && echo && exit 0
elif [ $DistroBasedOn == "Debian" ] ; then
	echo -n ": try apt-get install -y $TOOL" && echo && exit 0
elif [ $DistroBasedOn == "cygwin" ] ; then
	echo -n ": try cygwin setup, package name $TOOL" && echo && exit 0
elif [ $DistroBasedOn == "msys" ] ; then
	echo -n ": try pacman -S $TOOL" && echo && exit 0
fi
