Use the common utils + separate stable and canary installations

This commit is contained in:
TheElectronWill 2017-09-17 14:15:47 +02:00
parent d4215593a4
commit 7ade5025d9
9 changed files with 319 additions and 288 deletions

36
basic-checks.sh Normal file
View file

@ -0,0 +1,36 @@
#!/bin/bash
# It's a bad idea to run rpmbuild as root!
if [ "$(id -u)" = "0" ]; then
disp "$red_bg------------------------ WARNING ------------------------\n\
This script should NOT be executed with root privileges!\n\
Building rpm packages as root is dangerous and may harm the system!\n\
Actually, badly written RPM spec files may execute dangerous command in the system directories.\n\
So it is REALLY safer not to run this script as root.\n\
If you still want to continue, type \"do it!\" within 5 seconds (type anything else to exit)."
disp "------------------------ WARNING ------------------------$reset$bold"
read -t 5 -p '> Do you really want to do it (not recommended)? ' answer
if [ "$answer" != "do it!" ]; then
exit
fi
style $reset
fi
# Checks that the rpmbuild package is installed.
if ! type 'rpmbuild' > /dev/null; then
echo 'You need the rpm development tools to create rpm packages.'
style $bold
read -n 1 -p '> Install the rpmdevtools package now? [y/N]: ' answer
echo
case "$answer" in
y|Y)
sudo -p 'Enter your password to install rpmdevtools: ' dnf install rpmdevtools
;;
*)
echo "${reset}The package won't be installed. Exiting now."
exit
esac
style $reset
else
disp "${green}rpmbuild detected.$reset"
fi

90
common-functions.sh Normal file
View file

@ -0,0 +1,90 @@
#!/bin/bash
# ask_yesno question
## Asks a yes/no question and stores the result in the 'answer' variable
ask_yesno() {
style $reset$bold
read -n 1 -p "> $1 [y/N]: " answer
echo
style $reset
}
# ask_remove_dir directory
## Asks the user if they want to remove the specified directory, and removes it if they want to.
ask_remove_dir() {
ask_yesno "Remove the directory \"$1\"?"
case "$answer" in
y|Y)
rm -r "$1"
echo "Directory removed."
;;
*)
echo "Directory not removed."
esac
echo
}
# manage_dir directory directory_short_name
## If the specified directory exists, asks the user if they want to remove it.
## If it doesn't exist, creates it.
manage_dir() {
if [ -d "$1" ]; then
echo "The $2 directory already exist and may contain outdated data."
ask_remove_dir "$1"
fi
mkdir -p "$1"
}
# ask_installpkg
## Asks the user if they want to install the newly created package.
ask_installpkg() {
if [[ $1 == "all" || $2 == "all" ]]; then
pl='es'
else
pl='e'
fi
ask_yesno "Install the packag$pl now?"
case "$answer" in
y|Y)
cd "$rpm_dir/$arch"
if [[ $1 == "all" || $2 == "all" ]]; then
rpm_filename=$(find -type f -name '*.rpm' -printf '%P\n')
else
rpm_filename=$(find -maxdepth 1 -type f -name '*.rpm' -printf '%P\n' -quit)
fi
if [[ $1 == "allowerasing" || $2 == "allowerasing" ]]; then
sudo dnf install --allowerasing $rpm_filename
else
sudo dnf install "$rpm_filename"
fi
;;
*)
echo "Packag$pl not installed."
esac
}
# extract archive_file destination [options]
extract() {
echo "Extracting \"$1\"..."
if [[ "$1" == *.tar.gz ]]; then
command="tar -xzf \"$1\" -C \"$2\""
elif [[ "$1" == *.tar.xz ]];then
command="tar -xJf \"$1\" -C \"$2\""
elif [[ "$1" == *.tar.bz2 ]];then
command="tar -xjf \"$1\" -C \"$2\""
elif [[ "$1" == *.tar ]];then
command="tar -xf \"$1\" -C \"$2\""
elif [[ "$1" == *.zip ]]; then
command="unzip -q \"$1\" -d \"$2\""
else
disp "${red}Unsupported archive type for $1"
return 10
fi
if [ $# -eq 3 ]; then
eval $command $3 # Custom options
elif [ $# -eq 4 ]; then
eval $command $3 $4 # Custom options
else
eval $command
fi
}

View file

@ -1,138 +0,0 @@
#!/bin/sh
# Author: TheElectronWill
# This script downloads the latest version of Discord for linux, and creates a package with rpmbuild.
desktop_model="$PWD/discord.desktop"
spec_file="$PWD/discord-canary.spec"
rpm_dir="$PWD/RPMs"
work_dir="$PWD/work"
downloaded_dir="$work_dir/discord"
desktop_file="$work_dir/discord.desktop"
# It's a bad idea to run rpmbuild as root!
if [ "$(id -u)" = "0" ]; then
echo '------------------------ WARNING ------------------------'
echo 'This script should NOT be executed with root privileges!'
echo 'Building rpm packages as root is dangerous and may harm the system!'
echo 'Actually, badly written RPM spec files may execute dangerous command in the system directories.'
echo 'So it is REALLY safer not to run this script as root.'
echo 'If you still want to run this script as root, type "do it!" within 5 seconds (type anything else to exit):'
read -t 5 -p 'Do you really want to do it (not recommended)? ' answer
if [ "$answer" != "do it!" ]; then
exit
fi
echo '------------------------ WARNING ------------------------'
fi
# Checks that rpmbuild is installed.
if ! type 'rpmbuild' > /dev/null; then
echo 'You need the rpm development tools to create rpm packages.'
read -n 1 -p 'Do you want to install the rpmdevtools package now? [y/N]' answer
echo
case "$answer" in
y|Y)
sudo -p 'Enter your password to install rpmdevtools: ' dnf install rpmdevtools
;;
*)
echo "Ok, I won't install rpmdevtools."
exit
esac
else
echo "rpmbuild detected!"
fi
# Downloads the discord tar.gz archive and puts its name in the global variable archive_name.
download_discord() {
echo 'Downloading discord canary for linux...'
wget -q --show-progress --content-disposition 'https://discordapp.com/api/download/canary?platform=linux&format=tar.gz'
archive_name="$(ls *.tar.gz)"
}
# Asks the user if they want to remove the specified directory, and removes it if they want to.
ask_remove_dir() {
read -n 1 -p "Do you want to remove the \"$1\" directory? [y/N]" answer
echo
case "$answer" in
y|Y)
rm -r "$1"
echo "\"$1\" directory removed."
;;
*)
echo "Ok, I won't remove it."
esac
echo
}
# If the specified directory exists, asks the user if they want to remove it.
# If it doesn't exist, creates it.
manage_dir() {
if [ -d "$1" ]; then
echo "The $2 directory already exist. It may contain outdated data."
ask_remove_dir "$1"
fi
mkdir -p "$1"
}
manage_dir "$work_dir" 'work'
manage_dir "$rpm_dir" 'RPMs'
cd "$work_dir"
# Downloads discord if needed.
archive_name="$(ls *.tar.gz 2>/dev/null)"
if [ $? -eq 0 ]; then
echo "Found $archive_name"
read -n 1 -p 'Do you want to use this archive instead of downloading a new one? [y/N]' answer
echo
case "$answer" in
y|Y)
echo 'Ok, I will use this archive.'
;;
*)
rm "$archive_name"
download_discord
esac
else
download_discord
fi
echo
echo 'Extracting the files...'
archive_name="$(ls *.tar.gz)"
if [ ! -d "$downloaded_dir" ]; then
mkdir "$downloaded_dir"
fi
tar -xzf "$archive_name" -C "$downloaded_dir" --strip 1
# --strip 1 gets rid of the top archive's directory
# Gets the discord's version number + icon file name
echo 'Analysing the files...'
version_number="$(echo "$archive_name" | cut -d'-' -f3 | rev | cut -c 8- | rev)"
# Explaination on how it works:
# cut -d'-' -f3 splits the archive's name around the '-' character, and takes the 3rd part
# For example if archive_name is "discord-canary-0.0.10.tar.gz" we get "0.0.10.tar.gz"
# Then, rev | cut -c 8- | rev reverse the string, removes the first 7 characters, and re-reverse it.
# This actually removes the last 8 characters, ie the ".tar.gz" part.
# So in our example we'll get version_number=0.0.10
cd "$downloaded_dir"
icon_name="$(ls *.png)"
echo " Archive: $archive_name"
echo " Version: $version_number"
echo " Icon: $icon_name"
echo 'Creating .desktop file...'
sed "s/_version/$version_number/; s/_icon/$icon_name/; s/_exe/DiscordCanary/" "$desktop_model" > "$desktop_file"
echo 'Creating the RPM package (this may take a while)...'
rpmbuild --quiet -bb "$spec_file" --define "_topdir $work_dir" --define "_rpmdir $rpm_dir"\
--define "version_number $version_number" --define "downloaded_dir $downloaded_dir"\
--define "desktop_file $desktop_file"
echo
echo '------------------------- Done! -------------------------'
echo "The RPM package is located in the \"RPMs/x86_64\" folder."
ask_remove_dir "$work_dir"

View file

@ -1,138 +0,0 @@
#!/bin/sh
# Author: TheElectronWill
# This script downloads the latest version of Discord for linux, and creates a package with rpmbuild.
desktop_model="$PWD/discord.desktop"
spec_file="$PWD/discord-stable.spec"
rpm_dir="$PWD/RPMs"
work_dir="$PWD/work"
downloaded_dir="$work_dir/discord"
desktop_file="$work_dir/discord.desktop"
# It's a bad idea to run rpmbuild as root!
if [ "$(id -u)" = "0" ]; then
echo '------------------------ WARNING ------------------------'
echo 'This script should NOT be executed with root privileges!'
echo 'Building rpm packages as root is dangerous and may harm the system!'
echo 'Actually, badly written RPM spec files may execute dangerous command in the system directories.'
echo 'So it is REALLY safer not to run this script as root.'
echo 'If you still want to run this script as root, type "do it!" within 5 seconds (type anything else to exit):'
read -t 5 -p 'Do you really want to do it (not recommended)? ' answer
if [ "$answer" != "do it!" ]; then
exit
fi
echo '------------------------ WARNING ------------------------'
fi
# Checks that rpmbuild is installed.
if ! type 'rpmbuild' > /dev/null; then
echo 'You need the rpm development tools to create rpm packages.'
read -n 1 -p 'Do you want to install the rpmdevtools package now? [y/N]' answer
echo
case "$answer" in
y|Y)
sudo -p 'Enter your password to install rpmdevtools: ' dnf install rpmdevtools
;;
*)
echo "Ok, I won't install rpmdevtools."
exit
esac
else
echo "rpmbuild detected!"
fi
# Downloads the discord tar.gz archive and puts its name in the global variable archive_name.
download_discord() {
echo 'Downloading discord stable for linux...'
wget -q --show-progress --content-disposition 'https://discordapp.com/api/download?platform=linux&format=tar.gz'
archive_name="$(ls *.tar.gz)"
}
# Asks the user if they want to remove the specified directory, and removes it if they want to.
ask_remove_dir() {
read -n 1 -p "Do you want to remove the \"$1\" directory? [y/N]" answer
echo
case "$answer" in
y|Y)
rm -r "$1"
echo "\"$1\" directory removed."
;;
*)
echo "Ok, I won't remove it."
esac
echo
}
# If the specified directory exists, asks the user if they want to remove it.
# If it doesn't exist, creates it.
manage_dir() {
if [ -d "$1" ]; then
echo "The $2 directory already exist. It may contain outdated data."
ask_remove_dir "$1"
fi
mkdir -p "$1"
}
manage_dir "$work_dir" 'work'
manage_dir "$rpm_dir" 'RPMs'
cd "$work_dir"
# Downloads discord if needed.
archive_name="$(ls *.tar.gz 2>/dev/null)"
if [ $? -eq 0 ]; then
echo "Found $archive_name"
read -n 1 -p 'Do you want to use this archive instead of downloading a new one? [y/N]' answer
echo
case "$answer" in
y|Y)
echo 'Ok, I will use this archive.'
;;
*)
rm "$archive_name"
download_discord
esac
else
download_discord
fi
echo
echo 'Extracting the files...'
archive_name="$(ls *.tar.gz)"
if [ ! -d "$downloaded_dir" ]; then
mkdir "$downloaded_dir"
fi
tar -xzf "$archive_name" -C "$downloaded_dir" --strip 1
# --strip 1 gets rid of the top archive's directory
# Gets the discord's version number + icon file name
echo 'Analysing the files...'
version_number="$(echo "$archive_name" | cut -d'-' -f2 | rev | cut -c 8- | rev)"
# Explaination on how it works:
# cut -d'-' -f2 splits the archive's name around the '-' character, and takes the 2nd part
# For example if archive_name is "discord-0.0.1.tar.gz" we get "0.0.1.tar.gz"
# Then, rev | cut -c 8- | rev reverse the string, removes the first 7 characters, and re-reverse it.
# This actually removes the last 8 characters, ie the ".tar.gz" part.
# So in our example we'll get version_number=0.0.1
cd "$downloaded_dir"
icon_name="$(ls *.png)"
echo " Archive: $archive_name"
echo " Version: $version_number"
echo " Icon: $icon_name"
echo 'Creating .desktop file...'
sed "s/_version/$version_number/; s/_icon/$icon_name/; s/_exe/Discord/" "$desktop_model" > "$desktop_file"
echo 'Creating the RPM package (this may take a while)...'
rpmbuild --quiet -bb "$spec_file" --define "_topdir $work_dir" --define "_rpmdir $rpm_dir"\
--define "version_number $version_number" --define "downloaded_dir $downloaded_dir"\
--define "desktop_file $desktop_file"
echo
echo '------------------------- Done! -------------------------'
echo "The RPM package is located in the \"RPMs/x86_64\" folder."
ask_remove_dir "$work_dir"

104
create-package.sh Executable file
View file

@ -0,0 +1,104 @@
#!/bin/bash
# Author: TheElectronWill
# This script downloads the latest version of Discord for linux, and creates a package with rpmbuild.
source terminal-colors.sh # Adds color variables
source basic-checks.sh # Checks that rpmbuild is available and that the script isn't started as root
source common-functions.sh # Adds utilities functions
rpm_dir="$PWD/RPMs"
work_dir="$PWD/work"
downloaded_dir="$work_dir/discord"
desktop_model="$PWD/discord.desktop"
arch='x86_64'
# Checks that the version (stable/canary) is given as a parameter.
if [[ $# -ne 1 || $1 != "stable" && $1 != "canary" ]]; then
disp "${red}Wrong or missing parameters!$reset"
echo 'Usage: create-package.sh stable/canary'
exit
fi
discord_type="$1"
spec_file="$PWD/discord-$discord_type.spec"
if [[ $discord_type == "canary" ]]; then
app_name='Discord Canary'
exe_name='DiscordCanary'
download_url='https://discordapp.com/api/download/canary'
cut_part=3
desktop_file="$work_dir/discord-canary.desktop"
else
app_name='Discord'
exe_name='Discord'
download_url='https://discordapp.com/api/download'
cut_part=2
desktop_file="$work_dir/discord-stable.desktop"
fi
# Downloads the discord tar.gz archive and puts its name in the global variable archive_name.
download_discord() {
echo "Downloading discord $discord_type for linux..."
wget -q --show-progress --content-disposition "${download_url}?platform=linux&format=tar.gz"
archive_name="$(ls *.tar.gz)"
}
manage_dir "$work_dir" 'work'
manage_dir "$rpm_dir" 'RPMs'
cd "$work_dir"
# Downloads discord if needed.
archive_name="$(ls *.tar.gz 2>/dev/null)"
if [ $? -eq 0 ]; then
echo "Found the archive \"$archive_name\"."
ask_yesno 'Use this archive instead of downloading a new one?'
case "$answer" in
y|Y)
echo 'Existing archive selected.'
;;
*)
rm "$archive_name"
download_discord
esac
else
download_discord
fi
# Extracts the archive:
echo
if [ ! -d "$downloaded_dir" ]; then
mkdir "$downloaded_dir"
fi
extract "$archive_name" "$downloaded_dir" "--strip 1" # --strip 1 gets rid of the top archive's directory
# Gets the discord's version number + icon file name
echo 'Analysing the files...'
version_number="$(echo "$archive_name" | cut -d'-' -f$cut_part | rev | cut -c 8- | rev)"
# cut -d'-' -fn splits the archive's name around the '-' character, and takes the n-th part
# For example if archive_name is "discord-0.0.1.tar.gz" we get "0.0.1.tar.gz"
# Then, rev | cut -c 8- | rev reverse the string, removes the first 7 characters, and re-reverse it.
# This actually removes the last 8 characters, ie the ".tar.gz" part.
# So in our example we'll get version_number=0.0.1
cd "$downloaded_dir"
icon_name="$(ls *.png)"
echo " -> Version: $version_number"
echo " -> Icon: $icon_name"
echo 'Creating the .desktop file...'
sed "s/@version/$version_number/; s/@icon/$icon_name/; s/@exe/$exe_name/; s/@name/$app_name/; s/@type/$discord_type/"\
"$desktop_model" > "$desktop_file"
disp "${yellow}Creating the RPM package (this may take a while)..."
rpmbuild --quiet -bb "$spec_file" --define "_topdir $work_dir" --define "_rpmdir $rpm_dir"\
--define "version_number $version_number" --define "downloaded_dir $downloaded_dir"\
--define "desktop_file $desktop_file"
disp "${bgreen}Done!${reset_font}"
disp "The RPM package is located in the \"RPMs/$arch\" folder."
disp '----------------'
ask_remove_dir "$work_dir"
ask_installpkg

View file

@ -3,12 +3,12 @@
# downloaded_dir # downloaded_dir
# desktop_file # desktop_file
%define install_dir /opt/discord %define install_dir /opt/discord-canary
%define apps_dir /usr/share/applications %define apps_dir /usr/share/applications
Name: discord Name: discord-canary
Version: %{version_number} Version: %{version_number}
Release: canary%{?dist} Release: 1%{?dist}
Summary: Free Voice and Text Chat for Gamers. Summary: Free Voice and Text Chat for Gamers.
Group: Applications/Internet Group: Applications/Internet
@ -33,6 +33,7 @@ cp "%{desktop_file}" "%{buildroot}%{apps_dir}"
chmod +x "%{buildroot}%{install_dir}"/*.so chmod +x "%{buildroot}%{install_dir}"/*.so
%files %files
/* %{install_dir}
%{apps_dir}/*

View file

@ -3,12 +3,12 @@
# downloaded_dir # downloaded_dir
# desktop_file # desktop_file
%define install_dir /opt/discord %define install_dir /opt/discord-stable
%define apps_dir /usr/share/applications %define apps_dir /usr/share/applications
Name: discord Name: discord-stable
Version: %{version_number} Version: %{version_number}
Release: stable%{?dist} Release: 1%{?dist}
Summary: Free Voice and Text Chat for Gamers. Summary: Free Voice and Text Chat for Gamers.
Group: Applications/Internet Group: Applications/Internet
@ -33,6 +33,7 @@ cp "%{desktop_file}" "%{buildroot}%{apps_dir}"
chmod +x "%{buildroot}%{install_dir}"/*.so chmod +x "%{buildroot}%{install_dir}"/*.so
%files %files
/* %{install_dir}
%{apps_dir}/*

View file

@ -1,10 +1,10 @@
[Desktop Entry] [Desktop Entry]
Name=Discord Name=@name
Comment=Free voice and text chat for gamers Comment=Free voice and text chat for gamers
StartupWMClass=discord StartupWMClass=discord
Version=_version Version=@version
Icon=/opt/discord/_icon Icon=/opt/discord-@type/@icon
Exec=/opt/discord/_exe Exec=/opt/discord-@type/@exe
Type=Application Type=Application
Terminal=false Terminal=false
StartupNotify=true StartupNotify=true

75
terminal-colors.sh Normal file
View file

@ -0,0 +1,75 @@
#!/bin/bash
# Colors IDs
id_black=0
id_red=1
id_green=2
id_yellow=3
id_blue=4
id_purple=5
id_cyan=6
id_white=7
disp() { # echo -e
echo -e $@
}
style() { # echo -e -n
echo -e -n $@
}
code() { # Escape code
echo "\033[$1m"
}
fgr() { # Regular foreground color
let id=30+$1
if [ $# -eq 2 ]; then
data="$2;$id"
echo "$(code $data)"
else
echo "$(code $id)"
fi
}
bgr() { # Regular background color
let id=40+$1
echo "$(code $id)"
}
fgh() { # High-intensity foreground color
let id=90+$1
echo "$(code $id)"
}
bgh() { # High-intensity background color
let id=100+$1
echo "$(code $id)"
}
# Foreground colors Bold colors Underlined colors
black=$(fgr $id_black); bblack=$(fgr $id_black 1); ublack=$(fgr $id_black 4);
red=$(fgr $id_red); bred=$(fgr $id_red 1); ured=$(fgr $id_red 4);
green=$(fgr $id_green); bgreen=$(fgr $id_green 1); ugreen=$(fgr $id_green 4);
yellow=$(fgr $id_yellow); byellow=$(fgr $id_yellow 1); uyellow=$(fgr $id_yellow 4);
blue=$(fgr $id_blue); bblue=$(fgr $id_blue 1); ublue=$(fgr $id_blue 4);
purple=$(fgr $id_purple); bpurple=$(fgr $id_purple 1); upurple=$(fgr $id_purple 4);
cyan=$(fgr $id_cyan); bcyan=$(fgr $id_cyan 1); ucyan=$(fgr $id_cyan 4);
white=$(fgr $id_white); bwhite=$(fgr $id_white 1); uwhite=$(fgr $id_white 4);
# Background colors
black_bg=$(bgr $id_black)
red_bg=$(bgr $id_red)
green_bg=$(bgr $id_green)
yellow_bg=$(bgr $id_yellow)
blue_bg=$(bgr $id_blue)
purple_bg=$(bgr $id_purple)
cyan_bg=$(bgr $id_cyan)
white_bg=$(bgr $id_white)
# Effects
bold=$(code 1)
underline=$(code 4)
invert=$(code 7)
cross=$(code 9)
# Resets
reset=$(code 0) # resets all
reset_fg=$(code 39) # resets foreground color
reset_bg=$(code 49) # resets background color
reset_font=$(code '22;24') # resets font to regular, ie removes bold and underline