<commit_before>#!/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'-' -f3 | 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"<commit_msg>Fix empty version number when creating stable RPM<commit_after>#!/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"
{'fraction_non_alphanumeric': 0.14315245478036176, 'fraction_numerical': 0.008527131782945736, 'mean_word_length': 3.2957824639289677, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5797186', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2508, 'n_words': 1030}