Skip to content

Instantly share code, notes, and snippets.

@Kreyren
Last active January 6, 2020 22:05
Show Gist options
  • Save Kreyren/6a53705d479b36f3785b69a5ca6bd0ba to your computer and use it in GitHub Desktop.
Save Kreyren/6a53705d479b36f3785b69a5ca6bd0ba to your computer and use it in GitHub Desktop.
#!/bin/sh
# Created by Jacob Hrbek <kreyren@rixotstudio.cz> under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) in 2020
# Created for https://bugs.winehq.org/show_bug.cgi?id=45546#c26
MAINTAINER="kreyren@rixotstudio.cz"
# Simplified assertion
die() { printf 'FATAL: %s\n' "$2" ; exit "$1" ;}
# Simplified output handling
einfo() { printf 'INFO: %s\n' "$1" ;}
edebug() { [ -n "$DEBUG" ] && printf 'DEBUG: %s\n' "$1" ;}
# Instruct the end-user about bugs
einfo "This script has been created for https://bugs.winehq.org/show_bug.cgi?id=45546#c26, contact maintainer on $MAINTAINER if you encountered any issues"
# Patching
case "$1" in
[0-9][0-9][0-9][0-9][0-9])
# Fetch source
if [ ! -d /usr/src/wine ]; then
# File hierarchy check
if [ ! -d /usr ]; then
die 255 "This system doesn't have pathname '/usr' assuming unsupported file hierarchy, contact $MAINTAINER with details"
elif [ -d /usr ]; then
edebug "Directory '/usr' is present, assuming supported file hierarchy"
fi
# Source directory check
if [ ! -d /usr/src ]; then
if [ "$(id -u)" != 0 ]; then
if command -v sudo >/dev/null; then
sudo mkdir /usr/src || die 1 "Unable to make a new directory using sudo in /usr/src for source fetching"
elif ! command -v sudo >/dev/null; then
einfo "We are unable to make a new directory in '/usr/src' which is expected to be used for source fetching, inspect the script and if you don't see anything malicion then please enter your root password to work around root evelation for this directory"
su root -c "mkdir /usr/src || die 1 \"Unable to make a new directory in '/usr/src'\"" || die 3 "Unable to evelate root permission to make a new directory in /usr/src"
else
die 255 "Unexpected happend in checking for pathname '/usr/src', contact maintainer on $MAINTAINER with details"
fi
elif [ "$(id -u)" = 0 ]; then
mkdir /usr/src/wine || die 1 "Unable to make a new directory in '/usr/src'"
else
die 255 "User id is $(id -u) which is unexpected for creating directory '/usr/src', contact maintainer on $MAINTAINER with details"
fi
elif [ -d /usr/src ]; then
edebug "Directory '/usr/src' is present, skipping creation"
else
die 255 "Unexpected happend while checking for pathname /usr/src, contact maintainer on $MAINTAINER with details"
fi
# Source fetch
if [ ! -d /usr/src/wine ]; then
# Check if 'git' is installed
if ! command -v git >/dev/null; then die 126 "Command 'git' is not executable on this system, please install it to fetch wine source and re-invoke this script"; fi
# Clone
if [ "$(id -u)" != 0 ]; then
if command -v sudo >/dev/null; then
sudo git clone https://github.com/wine-mirror/wine.git /usr/src/wine || die 1 "Unable to clone wine source in ''/usr/src/wine'"
elif ! command -v sudo >/dev/null; then
einfo "We are unable to clone wine source in /usr/src/wine without root permission, inspect the script and if you don't see anything malicion then please parse your root password"
su root -c "git clone https://github.com/wine-mirror/wine.git /usr/src/wine || die 1 \"Unable to clone wine source in '/usr/src/wine'\"" || die 3 "Unable to elevate root permission to clone wine source in '/usr/src/wine'"
fi
elif [ "$(id -u)" = 0 ]; then
git clone https://github.com/wine-mirror/wine.git /usr/src/wine || die 1 "Unable to clone wine source in '/usr/src/wine'"
fi
elif [ -d /usr/src/wine ]; then
edebug "Directory '/usr/src/wine' already exists, skipping cloning"
else
einfo "Unexpected happend in cloning /usr/src/wine, contact the maintainer on $MAINTAINER with details"
fi
elif [ -f /usr/src/wine ]; then
die 1 "File '/usr/src/wine' is present where directory is expected, please rename and re-invoke this script"
elif [ -d /usr/src/wine ]; then
edebug "Directory /usr/src/wine is already present, skipping creation.."
elif [ -h /usr/src/wine ]; then
die 1 "File '/usr/src/wine' is a symbolic link which is unexpected, expecting a directory or non-existing pathname to be created"
else
die 255 "Unexpected happend while creating a directory '/usr/src/wine', contact $MAINTAINER with details"
fi
# Check for caching directory
if [ ! -d "$HOME/.cache" ]; then
mkdir "$HOME/.cache" || die 1 "Unable to make a new directory in '$HOME/.cache' used for caching wine patches"
# Inform the end-user about created directory
einfo "Directory '$HOME/.cache' has been created for caching"
elif [ -d "$HOME/.cache" ]; then
edebug "Directory '$HOME/.cache' is present, skipping creation"
elif [ -f "$HOME/.cache" ]; then
die 1 "Pathname '$HOME/.cache' is a file where directory is expected, rename the pathname and re-invoke the script, we need this path name for caching of wine patches"
elif [ -h "$HOME/.cache" ]; then
die 1 "Pathname '$HOME/.cache' is a symbolic link which is unexpected, expecting directory for caching"
else
die 255 "Unexpected happend in "
fi
# Create cache for this script
if [ ! -d "$HOME/.cache/wine-patches" ]; then
mkdir "$HOME/.cache/wine-patches" || die 1 "Unable to make a new directory in '$HOME/.cache/wine-patches' which is expected to be used for caching of wine patches"
elif [ -f "$HOME/.cache/wine-patches" ]; then
die 1 "Pathname '$HOME/.cache/wine-patches' is a file where directory for caching wine patches is expected"
elif [ -h "$HOME/.cache/wine-patches" ]; then
die 1 "Pathname '$HOME/.cache/wine-patches' is a symbolic link which is unexpected, expecting directory"
elif [ -d "$HOME/.cache/wine-patches" ]; then
edebug "Directory '$HOME/.cache/wine-patches' already exists, skipping creation"
else
die 255 "Unexpected happend in checking for '\$HOME/.cache/wine-patches', contact matainer on $MAINTAINER with details"
fi
# Cache wine-patch
if [ ! -f "$HOME/.cache/wine-patches/$1" ]; then
wget "https://bugs.winehq.org/attachment.cgi?id=$1" -O "$HOME/.cache/wine-patches/$1" || die 1 "Unable to fetch winepatch $1"
elif [ -f "$HOME/.cache/wine-patches/$1" ]; then
edebug "File $HOME/.cache/wine-patches/$1 already exists"
else
die 255 "Unexpected happend in applying patch $1 to /usr/src/wine"
fi
# Apply patch
if [ "$(id -u)" = 0 ]; then
if command -v sudo >/dev/null; then
patch -p1 --quiet --directory=/usr/src/wine < "$HOME/.cache/wine-patches/$1" || die 1 "Unable to apply patch '$HOME/.cache/wine-patches/$1' in /usr/src/wine, contact maintainer on $MAINTAINER with details"
elif ! command -v sudo >/dev/null; then
einfo "To apply patch in /usr/src/wine we need root permission, please inspect the script and if you don't see anything malicious then provide your root password"
su root -c "patch -p1 --quiet --directory=/usr/src/wine < \"$HOME/.cache/wine-patches/$1\" || die 1 \"Unable to apply patch '$HOME/.cache/wine-patches/$1' in /usr/src/wine, contact maintainer on $MAINTAINER with details\""
fi
elif [ "$(id -u)" != 0 ]; then
patch -p1 --quiet --directory=/usr/src/wine < "$HOME/.cache/wine-patches/$1" || die 1 "Unable to apply patch '$HOME/.cache/wine-patches/$1' in /usr/src/wine, contact maintainer on $MAINTAINER with details"
else
die 255 "Unexpected user ID $(id -u) has been parsed, contact maintainer on $MAINTAINER with details"
fi
einfo "Patch number $1 has been applied in /usr/src/wine"
# Return true
exit 0
;;
--help|-h|-help|help)
printf '%s\n' \
"Usage: path/to/script [NUMBER_OF_WINEPATCH]" \
"Applies wine patch to a wine source at /usr/src/wine" \
"" \
"Report bugs to: $MAINTAINER"
;;
*) die 2 "This script expects a one argument beeing number of the patch that is expected to be applied where '$1' was provided which is unsupported, contact maintainer on $MAINTAINER if you think that this should be supported"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment