yggdrasil_ng/core_functions.sh

534 lines
14 KiB
Bash
Raw Normal View History

2017-12-30 22:08:45 +00:00
#------------------------------------------------------------------------------#
# Yggdrasil NG #
# compatibility : Mint 18, Ubuntu 16.04, Elementary and other derivatives #
# author : Francois B. (Makotosan/Shakasan) #
# licence : GPLv3 #
# website : https://makotonoblog.be/
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# core functions #
#------------------------------------------------------------------------------#
#
# display a simple message + CR
#
function smsgn () {
printf "$*\n"
}
#
# display a simple message
#
function smsg () {
printf "$*"
}
#
# display a message + notification
#
function msg () {
printf "\n"
printf "$JAUNE"
if [ "$#" -gt "0" ]; then
printf "$*\n"
/usr/bin/notify-send -t 7000 "$*"
fi
printf "$NORMAL"
}
#
# display a message between [ ] depending of the ret_code
#
function retCode () {
typeset ret_code="$1"
if [ $ret_code == 0 ]; then
printf "[ ""$BOLDVERT""OK"$NORMAL" ] "
else
printf "[ ""$BOLDROUGE""!!"$NORMAL" ] "
fi
2018-02-24 16:48:53 +00:00
printf "\n"
2017-12-30 22:08:45 +00:00
}
#
# run a shell command and display a message between [ ] depending on the ret_code
#
function runCmd () {
typeset cmd="$1"
typeset ret_code
printf "[CMD] executing : $cmd "
printf "\n[CMD] executing $cmd\n" &>> $logFile
eval $cmd" &>> $logFile"
ret_code=$?
retCode $ret_code
}
#
# display a message + notification + ask to push a key to continue
#
function pressKey () {
printf "$*\n"
if which mpg123 >/dev/null; then
mpg123 -q $scriptDir/notify.mp3 &
fi
printf "$INV"
read -p "Press <Enter> key to continue ..."
printf "$NORMAL"
}
#
# check if OS is Mint
#
function isMint () {
OS=`lsb_release -d | awk -F':' '{print $2}' | awk -F'\t' '{print $2}'`
if [[ $OS == *"Linux Mint 18"* ]]; then
return 0
else
return 1
fi
}
#
# check if DE is Mate
#
function isMate () {
if [[ $DESKTOP_SESSION == *"mate"* ]]; then
return 0
else
return 1
fi
}
2018-03-14 18:34:00 +00:00
#
# check and install required dependencies for Yggdrasil
2018-03-14 18:55:39 +00:00
# TODO: update npm before update/install pkg
2018-03-14 18:34:00 +00:00
#
function yggInit () {
2018-03-16 19:34:10 +00:00
typeset ret_code
printf "[APT] update "
printf "\n[APT] update\n" &>> $logFile
sudo apt-get update &>> $logFile
ret_code=$?
retCode $ret_code
2018-03-14 18:34:00 +00:00
if ! which gem >/dev/null; then
printf "[INIT][GEM] not found, installing...\n"
printf "\n[INIT][GEM] not found, installing...\n" &>> $logFile
installPackage apt "ruby-dev"
else
printf "[INIT][GEM] found [ ""$BOLDVERT""OK"$NORMAL" ] \n"
fi
if ! which snap >/dev/null; then
printf "[INIT][SNAP] not found, installing...\n"
printf "\n[INIT][SNAP] not found, installing...\n" &>> $logFile
installPackage apt "snapd"
else
printf "[INIT][SNAP] found [ ""$BOLDVERT""OK"$NORMAL" ] \n"
fi
if ! which npm >/dev/null; then
printf "[INIT][NPM] not found, installing...\n"
printf "\n[INIT][NPM] not found, installing...\n" &>> $logFile
installPackage apt "npm"
else
printf "[INIT][NPM] found [ ""$BOLDVERT""OK"$NORMAL" ] \n"
fi
printf "[INIT]"
installPackage apt "apt-transport-https"
if ! which pip3 >/dev/null; then
printf "[INIT][PIP] not found, installing...\n"
printf "\n[INIT][PIP] not found, installing...\n" &>> $logFile
printf "[INIT]"
installPackage apt "python3-pip"
printf "[INIT]"
installPackage pip "pip"
printf "[INIT]"
installPackage pip "setuptools"
else
printf "[INIT][PIP] found [ ""$BOLDVERT""OK"$NORMAL" ] \n"
printf "[INIT]"
installPackage pip "pip"
printf "[INIT]"
installPackage pip "setuptools"
fi
}
2017-12-30 22:08:45 +00:00
#
# system update
2018-01-03 21:59:50 +00:00
#TODO: add pip, npm, gem
2017-12-30 22:08:45 +00:00
#
function updateSystem () {
typeset ret_code
printf "[APT] update "
printf "\n[APT] update\n" &>> $logFile
sudo apt-get update &>> $logFile
ret_code=$?
retCode $ret_code
printf "[APT] upgrade "
printf "\n[APT] upgrade\n" &>> $logFile
sudo apt-get -y upgrade &>> $logFile
ret_code=$?
retCode $ret_code
printf "[APT] dist-upgrade "
printf "\n[APT] dist-upgrade\n" &>> $logFile
sudo apt-get -y dist-upgrade &>> $logFile
ret_code=$?
retCode $ret_code
2018-03-14 18:34:00 +00:00
if which snap >/dev/null; then
printf "[SNAP] refresh "
printf "\n[SNAP] refresh\n" &>> $logFile
sudo snap refresh &>> $logFile
2018-03-14 11:10:56 +00:00
ret_code=$?
2018-03-14 18:34:00 +00:00
retCode $ret_code
2018-03-14 11:10:56 +00:00
fi
2018-01-01 15:58:02 +00:00
repoAdded=0
2017-12-30 22:08:45 +00:00
}
#
# add ppa
# input : ppa name
#
function addPPA () {
typeset ret_code
printf "[PPA] adding : $* "
printf "\n[PPA] adding $*\n" &>> $logFile
sudo add-apt-repository -y $* &>> $logFile
ret_code=$?
retCode $ret_code
}
#
# add repo's key
# input : file's url OR keyserver + key
#
function addKey () {
typeset ret_code
case $# in
"1")
printf "[REPO] adding key from file : $1 "
printf "\n[REPO] adding key from file $1\n" &>> $logFile
wget -qO - $1 | sudo apt-key add - &>> $logFile
ret_code=$?
retCode $ret_code
;;
"2")
printf "[REPO] adding key $2 from $1 "
printf "\n[REPO] adding key $2 from $1\n" &>> $logFile
2018-03-16 03:57:06 +00:00
#sudo apt-key adv --keyserver $1 --recv-keys $2 - &>> $logFile
gpg --keyserver $1 --recv-keys $2 &>> $logFile \
2018-03-16 03:57:06 +00:00
&& gpg -a --export $2 | sudo apt-key add - &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
;;
esac
}
#
# create new entry in /etc/apt/source.list.d/
# input : filename, repo, src repo
#
function addRepo () {
typeset ret_code
2018-03-14 11:22:50 +00:00
printf "[REPO] adding : $2 in $1 "
2017-12-30 22:08:45 +00:00
printf "\n[REPO] adding $2 in $1\n" &>> $logFile
echo $2 | sudo tee /etc/apt/sources.list.d/$1 &>> $logFile
ret_code=$?
retCode $ret_code
if [ "$#" -eq 3 ]; then
2018-03-14 11:22:50 +00:00
printf "[REPO] adding : $3 in $1 "
2017-12-30 22:08:45 +00:00
printf "\n[REPO] adding $3 in $1\n" &>> $logFile
echo $3 | sudo tee -a /etc/apt/sources.list.d/$1 &>> $logFile
ret_code=$?
retCode $ret_code
fi
}
#
# install package manually
# input : url of package, package name
#
function installPackageDpkg () {
2018-03-14 18:44:29 +00:00
printf "[PKG] Installing $2 from $1 "
printf "\n[PKG] installing $2 from $1\n" &>> $logFile
cd /tmp \
&& wget -q -O $2 $1 \
&& sudo dpkg -i $2 \
&& sudo apt-get install -fy &>> $logFile
ret_code=$?
retCode $ret_code
2017-12-30 22:08:45 +00:00
}
#
# install package from repositories
# input : package manager, package name
# package manager available : apt, pip, npm, gem, snap
# TODO: add apm, flatpak, umake?
#
function installPackage () {
typeset pkg="$2"
2017-12-30 22:08:45 +00:00
typeset ret_code
case $1 in
"apt")
2018-02-13 09:58:11 +00:00
printf "[APT] Installing $pkg "
printf "\n[APT] installing $pkg\n" &>> $logFile
2018-01-03 22:20:50 +00:00
sudo apt-get install -fy $pkg &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
;;
"pip")
if which pip3 >/dev/null; then
2018-02-13 09:58:11 +00:00
printf "[PIP] Installing $pkg "
printf "\n[PIP] installing $pkg\n" &>> $logFile
2018-01-03 22:20:50 +00:00
sudo -H pip3 install --upgrade $pkg &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
fi
;;
"npm")
if which npm >/dev/null; then
2018-02-13 09:58:11 +00:00
printf "[NPM] Installing $pkg "
printf "\n[NPM] installing $pkg\n" &>> $logFile
2018-01-03 22:20:50 +00:00
sudo npm install -g $pkg &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
fi
;;
"gem")
if which gem >/dev/null; then
2018-02-13 09:58:11 +00:00
printf "[GEM] Installing $pkg "
printf "\n[GEM] installing $pkg\n" &>> $logFile
2018-01-03 22:20:50 +00:00
sudo gem install $pkg &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
fi
;;
"snap")
if which snap >/dev/null; then
2018-02-13 09:58:11 +00:00
printf "[SNAP] Installing $pkg "
printf "\n[SNAP] installing $pkg\n" &>> $logFile
sudo snap install $pkg --classic &>> $logFile
2017-12-30 22:08:45 +00:00
ret_code=$?
retCode $ret_code
fi
;;
esac
}
#
# check dependency and install it if necessary
2018-03-14 18:50:07 +00:00
# input : package manager, package name, cmd/bin to check
2018-03-14 18:53:53 +00:00
#
2017-12-30 22:08:45 +00:00
function checkAndInstallDep () {
2018-03-14 18:50:07 +00:00
if ! which $3 >/dev/null; then
printf "[DEP] dependency $2 not found [ ""$BOLDROUGE""!!"$NORMAL" ]\n"
printf "[DEP] dependency $2 not found\n" &>> $logFile
2017-12-30 22:08:45 +00:00
case $1 in
"apt")
installPackage apt $2
;;
"npm")
installPackage npm $2
;;
"pip")
installPackage pip $2
;;
"gem")
installPackage gem $2
;;
"snap")
installPackage snap $2
;;
esac
fi
}
#
# add specific repository for a given package
# input : unique ID, apt cache update yes/no
#
function addSpecificRepoFct () {
for i in $AppsRepo; do
appRepo=(${i//;/ })
if [ "${appRepo[0]}" == "$1" ]; then
printf "[ADD_REPO_FCT] for package : $1, "
printf "add repo by function : ${appRepo[1]} \n"
eval "${appRepo[1]}"
2018-01-01 15:58:02 +00:00
repoAdded=$(($repoAdded+1))
2017-12-30 22:08:45 +00:00
fi
done
if [ "$2" == "yes" ]; then
updateSystem
fi
2018-02-24 21:12:20 +00:00
unset i
unset appRepo
2017-12-30 22:08:45 +00:00
}
#
# find and execute Pre/Post install functions for a specific app
# input : unique ID
#
function processAppTrtFct () {
for i in $AppsTrtFct; do
appTrtFct=(${i//;/ })
if [ "${appTrtFct[0]}" == "$1" ]; then
printf "[APP_TRT_FCT] for package : $1, "
printf "processing function : ${appTrtFct[1]} \n"
eval "${appTrtFct[1]}"
fi
done
}
#
# install all about a specific category from Apps array app list (headless)
# input : category name
#
function installAppsFromList () {
msg "Installing apps from $1 category"
for i in $Apps; do
app=(${i//;/ })
if [ "${app[2]}" == "$1" ]; then
addSpecificRepoFct ${app[3]}
fi
done
unset i
unset app
2018-01-01 15:58:02 +00:00
if [ "$repoAdded" -gt "0" ]; then
updateSystem
fi
2017-12-30 22:08:45 +00:00
for i in $Apps; do
app=(${i//;/ })
if [ "${app[2]}" == "$1" ]; then
installPackage ${app[1]} ${app[0]}
processAppTrtFct ${app[3]}
fi
done
}
#
# generate install apps menu about a specific category from Apps array app list (Menu)
# input : category name
#
function installAppsFromListMenu () {
for i in $Apps; do
app=(${i//;/ })
if [ "${app[2]}" == "$1" ]; then
list+=("${app[3]}" "${app[3]}" "ON")
fi
done
pkg=$(whiptail \
--title "Yggdrasil $version - App Install ($1)" \
--checklist \
"Select app to install and press OK" 25 80 19 \
"${list[@]}" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
for pkgToInstall in $pkg; do
for i in $Apps; do
app=(${i//;/ })
if [ "${app[3]}" == "${pkgToInstall//\"}" ]; then
addSpecificRepoFct ${app[3]}
fi
done
done
unset i
unset app
unset pkgToInstall
if [ "$repoAdded" -gt "0" ]; then
updateSystem
fi
2017-12-30 22:08:45 +00:00
for pkgToInstall in $pkg; do
for i in $Apps; do
app=(${i//;/ })
if [ "${app[3]}" == "${pkgToInstall//\"}" ]; then
installPackage ${app[1]} ${app[0]}
processAppTrtFct ${app[3]}
fi
done
done
fi
unset list
pressKey
}
#
# create app shortcut
# inputs : bin to exec, icon path, category, name, local/global, start w/ session
#
function createAppShortcut () {
typeset ret_code
shortcut='[Desktop Entry]\n'
shortcut+='Encoding=UTF-8\n'
shortcut+='Terminal=0\n'
shortcut+='Exec='$1'\n'
shortcut+='Icon='$2'\n'
shortcut+='Type=Application\n'
if [ "$6" == "start" ]; then
shortcut+='X-MATE-Autostart-enabled=true\n\n'
else
shortcut+='Categories='$3';\n'
fi
shortcut+='StartupNotify=true\n'
shortcut+='Name='$4'\n'
shortcut+='GenericName='$4'\n'
shortcut+='Comment='
if [ "$5" == "global" ]; then
smsg "creating shortcut for $4 "
echo -e $shortcut > /usr/share/applications/"$4".desktop
ret_code=$?
retCode $ret_code
else
mkdir -p /home/$myHomedir/.local/share/applications
smsg "creating shortcut for $4 "
echo -e $shortcut > /home/"$myHomedir"/.local/share/applications/"$4".desktop
ret_code=$?
retCode $ret_code
fi
}
#
# show Yggdrasil logo
#
function dispLogo () {
printf "\n"
printf "$BOLDJAUNE"
printf "██╗ ██╗ ██████╗ ██████╗ ██████╗ ██████╗ █████╗ ███████╗██╗██╗ \n"
printf "╚██╗ ██╔╝██╔════╝ ██╔════╝ ██╔══██╗██╔══██╗██╔══██╗██╔════╝██║██║ \n"
printf " ╚████╔╝ ██║ ███╗██║ ███╗██║ ██║██████╔╝███████║███████╗██║██║ \n"
printf " ╚██╔╝ ██║ ██║██║ ██║██║ ██║██╔══██╗██╔══██║╚════██║██║██║ \n"
printf " ██║ ╚██████╔╝╚██████╔╝██████╔╝██║ ██║██║ ██║███████║██║███████╗\n"
printf " ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝╚══════╝\n"
printf "$BOLDROUGE Customize Linux Mint & Ubuntu derivatives made easier\n"
printf "$BOLDBLANC ver "$version" - GPLv3 - Francois B. (Makotosan) - makotonoblog.be\n"
printf "\n"
}
#
# show usage for headless mode
#
function usage () {
dispLogo
printf "$NORMAL"
printf "Usage : yggdrasil [options]\n"
2018-01-05 06:14:58 +00:00
printf " -f : full install (see doc for more details)\n"
2017-12-30 22:08:45 +00:00
printf " -c : install gtk themes and icons\n"
2018-01-03 21:59:50 +00:00
printf " -u : update system (apt,snap,...)\n"
2017-12-30 22:08:45 +00:00
printf " -v : show verison number\n"
printf " -h : show help & informations\n"
}