Skip to content

Commit

Permalink
Rearranges the code for better viewing
Browse files Browse the repository at this point in the history
No change in functionality
  • Loading branch information
sudo-give-me-coffee committed Jan 20, 2020
1 parent 585b70c commit 046054a
Showing 1 changed file with 119 additions and 67 deletions.
186 changes: 119 additions & 67 deletions data/WineLauncher
@@ -1,75 +1,127 @@
#!/bin/bash

function cleanup() {
if [ -d "${WINEPREFIX}/drive_c" ]; then
find "${WINEPREFIX}/drive_c" -maxdepth 1 -type l | sed 's|^|unlink "|' | sed 's|$|"|' | sh
fi
if [ -f "${WINEPREFIX}/.update-timestamp" ]; then
echo "disabled" > "${WINEPREFIX}/.update-timestamp"
fi
}

[ "${XDG_CONFIG_HOME}" = "" ] && export XDG_CONFIG_HOME="${HOME}/.config"

export HERE="$(dirname "$(readlink -f "${0}")")"
export APP_ID=$(basename -s .desktop $(ls "${HERE}/"*".desktop"))
export APP_PATH=$(cat "${HERE}/${APP_ID}.desktop" | grep ^"X-WineExec=" | cut -c 13- | sed 's/.$//')
export WINEPREFIX="${XDG_CONFIG_HOME}/${APP_ID}"
export APP_FILES=$(find "${HERE}/app" -maxdepth 1 | sed '1d')
export LD_LIBRARY_PATH="${HERE}/usr/lib":${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu":${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH="${HERE}/lib":${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH="${HERE}/lib/i386-linux-gnu":${LD_LIBRARY_PATH}
export LIBGL_DRIVERS_PATH="${HERE}/usr/lib/i386-linux-gnu/dri"
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/pulseaudio":${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/alsa-lib":${LD_LIBRARY_PATH}
export FONTCONFIG_PATH="${HERE}/etc/fonts"
export WINELDLIBRARY="${HERE}/lib/ld-linux.so.2"
export LD_PRELOAD="${HERE}/bin/libhookexecv.so"

mkdir -p "${WINEPREFIX}/drive_c"
#!/usr/bin/env bash
#
# Launcher of Application
#
# Where to find libraries and stuff
function setup_environment_variables(){
# AppDir
export HERE="$(dirname "$(readlink -f "${0}")")"
# Apps
export APP_ID=$(basename -s .desktop $(ls "${HERE}/"*".desktop"))
export APP_PATH=$(cat "${HERE}/${APP_ID}.desktop" | grep ^"X-WineExec=" \
| cut -c 16- \
| sed 's/.$//')
export APP_FILES=$(find "${HERE}/app" -maxdepth 1 | sed '1d')
# Link dynamic
export LD_DEBUG_OUTPUT=/dev/null
export LD_LIBRARY_PATH="${HERE}/usr/lib":"${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu":"${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/lib":"${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/lib/i386-linux-gnu":"${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/pulseaudio":"${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib/i386-linux-gnu/alsa-lib":"${LD_LIBRARY_PATH}"
# Mesa 3D
export LIBGL_DRIVERS_PATH="/usr/lib/i386-linux-gnu/:${HERE}/usr/lib/i386-linux-gnu/dri"
# Font anti aliasing
export FONTCONFIG_PATH="${HERE}/etc/fonts"
# Wine
export WINEDLLOVERRIDES="mscoree,mshtml=" # Do not ask to install Mono or Gecko
export WINEDEBUG=-all # Do not print Wine debug messages
export WINELDLIBRARY="${HERE}/lib/ld-linux.so.2"
export WINEPREFIX="${XDG_CONFIG_HOME}/${APP_ID}"
}

trap cleanup EXIT
cleanup
# Helper functions
function clear_links() {
[ -d "${WINEPREFIX}/drive_c" ] && {
find "${WINEPREFIX}/drive_c" -maxdepth 1 -type l | \
sed 's|^|unlink "|' \
| sed 's|$|"|' | sh

if [ "$(cat ${HERE}/copy_app_files)" == "yes" ]; then
if [ ! -f "${WINEPREFIX}/already_copied" ]; then
echo "${APP_FILES}" | grep -v "windows"$ | sed "s/^/cp -vr \"/g" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh
touch "${WINEPREFIX}/already_copied"
fi
export APP_FILES=$(echo "${APP_FILES}" | grep "windows"$)
fi
[ -f "${WINEPREFIX}/.update-timestamp" ] && {
echo "disabled" > "${WINEPREFIX}/.update-timestamp"
}
}
}

function create_links(){
echo "${APP_FILES}" | sed "s|^|ln -fs \"|" | \
sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh
}

function wine_run_regedit(){
LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wineserver" --persistent=1 > grep -v ":fixme:"
LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wine" regedit "${WINEPREFIX}/default.reg"
}

function monitorate_app_process(){
INVALID_APP_STATUS=( "" "Z" )
while true; do
[ "${APP_PID}" = "" ] && {
APP_PID=$(ps -e -o pid,cmd | $(which grep) "${HERE}/app/${APP_PATH}" \
| $(which grep) -v "$(which grep)" \
| sed 's/^ //' | cut -d' ' -f1)
}
APP_STATUS=$(ps -q $APP_PID -o state --no-headers)
if [[ " ${INVALID_APP_STATUS[@]} " =~ " ${APP_STATUS} " ]]; then
LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" "${HERE}/bin/wineserver" -k
exit
fi
sleep 0.3
done
}

echo "${APP_FILES}" | sed "s|^|ln -s \"|" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh
# Main functon
function main(){
# Ensures that prefix base directory exists
mkdir -p "${WINEPREFIX}/drive_c"

# Checks if the application needs to run in your directory
[ "$(cat ${HERE}/change-directory)" == "yes" ] && {
cd $(dirname "${HERE}/app/${APP_PATH}")
}

# Checks if the application needs to be in a writable directory
[ "$(cat ${HERE}/copy_app_files)" == "yes" ] && {
[ ! -f "${WINEPREFIX}/already_copied" ] && {
echo "${APP_FILES}" | grep -v "windows"$ | sed "s/^/cp -vr \"/g" | sed "s|$|\" ${WINEPREFIX}/drive_c|" | sh
touch "${WINEPREFIX}/already_copied"
}
export APP_FILES=$(echo "${APP_FILES}" | grep "windows"$)
}

# Shortcuts
clear_links
create_links

# Checks if registry must be created
[ ! -f "${WINEPREFIX}/system.reg" ] && {
local TODAY="$(date '+%Y%m%d')"
cp "${HERE}/improve.reg" "${WINEPREFIX}/default.reg"
sed -i "s/{Install date here}/\"InstallDate\"=\"${TODAY}\"/g" "${WINEPREFIX}/default.reg"
wine_run_regedit
}

# Starts the application
LD_PRELOAD="${HERE}/bin/libhookexecv.so" "${WINELDLIBRARY}" \
"${HERE}/bin/wine" \
"${HERE}/app/${APP_PATH}" ${*} &

if [ ! -f "${WINEPREFIX}/system.reg" ]; then
export TODAY="$(date '+%Y%m%d')"
cp "${HERE}/improve.reg" "${WINEPREFIX}/default.reg"
sed -i "s/{Install date here}/\"InstallDate\"=\"${TODAY}\"/g" "${WINEPREFIX}/default.reg"
"${WINELDLIBRARY}" "${HERE}/bin/wineserver" --persistent=1
"${HERE}/wine" create-register "${WINEPREFIX}/default.reg"
fi
# Kill the wine server after the application's end
monitorate_app_process
}

if [ "$(cat ${HERE}/change-directory)" == "yes" ]; then
cd "$(dirname "${APP_PATH}")"
fi
# Ensures that XDG_CONFIG_HOME is defined
[ -z "${XDG_CONFIG_HOME}" ] && {
export XDG_CONFIG_HOME="${HOME}/.config"
}

APP_PATH=$(echo "${HERE}/app/$(echo "${APP_PATH}" | cut -c 4-)")
"${WINELDLIBRARY}" "${HERE}/bin/wine" "${APP_PATH}" ${*} 2>&1 | grep -v ":fixme:" &
# Clears links at exit
trap clear_links EXIT

INVALID_APP_STATUS=( "" "Z" )
# Initializes environment
setup_environment_variables
args=("$@")

# Kill wineserver after app closes
while true; do
[ "${APP_PID}" = "" ] && {
APP_PID=$(ps -e -o pid,cmd | $(which grep) "${APP_PATH}" \
| $(which grep) -v "$(which grep)" \
| sed 's/^ //' | cut -d' ' -f1)
}
APP_STATUS=$(ps -q $APP_PID -o state --no-headers)
if [[ " ${INVALID_APP_STATUS[@]} " =~ " ${APP_STATUS} " ]]; then
"${WINELDLIBRARY}" "${HERE}/bin/wineserver" -k
exit
fi
sleep 0.3
done
# Call initialization processes
main

0 comments on commit 046054a

Please sign in to comment.