Skip to content

Commit

Permalink
Add repair.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed May 6, 2024
1 parent 847ecf9 commit ea6f25e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
3 changes: 1 addition & 2 deletions make-package.sh
Expand Up @@ -53,13 +53,12 @@ mkdir -p "$basedir"
cp version "$basedir/package-version"
cp src/scripts/uninstall.sh "$basedir"
cp src/scripts/uninstall_core.sh "$basedir"
cp src/scripts/repair.sh "$basedir"
cp files/complex_modifications_rules_example.json "$basedir"
cp -R "src/apps/AppIconSwitcher/build/Release/Karabiner-AppIconSwitcher.app" "$basedir"
cp -R "src/apps/Menu/build/Release/Karabiner-Menu.app" "$basedir"
cp -R "src/apps/MultitouchExtension/build/Release/Karabiner-MultitouchExtension.app" "$basedir"
cp -R "src/apps/NotificationWindow/build/Release/Karabiner-NotificationWindow.app" "$basedir"
# Save a copy of Karabiner-Elements.app so that we can restore it if /Applications/Karabiner-Elements.app is manually deleted.
cp -R "src/apps/SettingsWindow/build/Release/Karabiner-Elements.app" "$basedir"

basedir="pkgroot/Library/Application Support/org.pqrs/Karabiner-Elements/scripts"
mkdir -p "$basedir"
Expand Down
6 changes: 6 additions & 0 deletions pkginfo/Scripts/postinstall
Expand Up @@ -39,4 +39,10 @@ launchctl enable system/org.pqrs.karabiner.karabiner_grabber
launchctl bootstrap system /Library/LaunchDaemons/org.pqrs.karabiner.karabiner_grabber.plist
launchctl enable system/org.pqrs.karabiner.karabiner_grabber

#
# Create a backup to allow recovery if the user manually deletes Karabiner-Elements without using the uninstaller
#

tar -C /Applications -cf '/Library/Application Support/org.pqrs/Karabiner-Elements/Karabiner-Elements.app.tar.gz' Karabiner-Elements.app

exit 0
21 changes: 2 additions & 19 deletions src/core/grabber/include/grabber/main/daemon.hpp
Expand Up @@ -43,27 +43,10 @@ int daemon(void) {
}

//
// Restore /Applications/Karabiner-Elements.app if it was removed manually
// Run repair.sh
//

if (!std::filesystem::exists("/Applications/Karabiner-Elements.app")) {
logger::get_logger()->info("Restore /Applications/Karabiner-Elements.app");

// In order to have it registered with launch services,
// we have to copy application files instead of creating symlink at /Applications/Karabiner-Elements.app.

auto copy_options = std::filesystem::copy_options::update_existing |
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::copy_symlinks;
std::error_code error_code;
std::filesystem::copy("/Library/Application Support/org.pqrs/Karabiner-Elements/Karabiner-Elements.app",
"/Applications/Karabiner-Elements.app",
copy_options,
error_code);
if (error_code) {
logger::get_logger()->error("Failed to restore /Applications/Karabiner-Elements.app: {0}", error_code.message());
}
}
system("/bin/sh '/Library/Application Support/org.pqrs/Karabiner-Elements/repair.sh'");

//
// Check Karabiner-Elements.app exists
Expand Down
10 changes: 10 additions & 0 deletions src/scripts/repair.sh
@@ -0,0 +1,10 @@
#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

if [ ! -d /Applications/Karabiner-Elements.app ]; then
echo "Restore /Applications/Karabiner-Elements.app"

tar -C /Applications -xf '/Library/Application Support/org.pqrs/Karabiner-Elements/Karabiner-Elements.app.tar.gz'
fi

0 comments on commit ea6f25e

Please sign in to comment.