Skip to content. | Skip to navigation

FreeSMUG

Sections
Personal tools
You are here: Home OS X Portable Apps Source Code Portable X-Chat Aqua source code
Document Actions

Portable X-Chat Aqua source code

by Gand last modified 2006-05-17 01:51 PM
Portable X-chat Aqua script code:

Shell Script (Release 1.0)

Portable X-Caht Aqua.app is packaged using the Platypus script wrapper from
http://sveinbjorn.sytes.net/platypus


#!/bin/sh

##########################################################################
#
# Portable X-Chat Aqua
#
# $Revision: 1.0
#
# The Contents of this file are made available subject to the terms of
# either of the following licenses
#
# - GNU General Public License Version 2.1
#
# Carlo Gandolfi, Paolo Portabluri, May 2006
#
# GNU General Public License Version 2.1
# =============================================
# Copyright 2006 by:
# Carlo Gandolfi - http://www.freesmug.org
# Paolo Portaluri - http://plus2.it/~paolo/
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License version 2.1, as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
##########################################################################


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

appurl=".xchat2"
appid="X-Chat Aqua"

# userplistbase=
# .xchat2 is a link to userpref
userplist="$HOME/$appurl"
userprefbase="Library/Application Support"
userpref="$HOME/$userprefbase/$appid"

p_userbase="$1/Contents/Resources/app"
p_userplist="$p_userbase/$appurl"
p_userpref="$p_userbase/$userprefbase/$appid"

copy_pref="$p_userbase/CopyPref_Done"


# ========================================================
# quitapp
# Check if local app is open and quit it
# ========================================================

function quitapp {
if ps cx | grep '[0-9] '"$appid"'$' > /dev/null; then
message1="A copy $appid is already running. Do you want to quit the running application and open Portable $appid instead?"
button=`/usr/bin/osascript << EOT
tell application "Finder"
beep
display dialog "$message1" buttons {"Cancel", "Continue"} \
with title "Qiut Running $appid" with icon caution default button "Continue"
set result to button returned of result
end tell
EOT`
if [ "$button" == "Continue" ]; then # Quit local application
osascript -e 'tell app "'"$appid"'" to quit'
else
exit 0
fi
fi
}

# ========================================================
# repair
# Repair preferences if Portable Application crashed
# ========================================================

function repair {
if [ -L "$userplist".pabu ] || [ -d "$userpref".pabu ] ; then
message2="Portable $appid has crashed without restoring existing preferences. \
Do you want to restore the original preferences now \
or copy to BackUp folder to manually restore later?"
button=`/usr/bin/osascript << EOT
tell application "Finder"
beep
display dialog "$message2" buttons {"Continue", "Restore", "Copy"} \
with title "Restoring Existing Preferences" \
with icon stop default button "Copy"
set result to button returned of result
end tell
EOT`
if [ "$button" == "Copy" ]; then # Save old preferences in BackUp folder
rm -rf "$HOME/$appid Pref BackUp/"
mkdir -p "$HOME/$appid Pref BackUp/$userprefbase"
mv "$userplist".pabu "$HOME/$appid Pref BackUp/$appurl"
mv "$userpref".pabu "$HOME/$appid Pref BackUp/$userprefbase/$appid"
elif [ "$button" == "Restore" ]; then # Restore existing preference
if [ -L "$userplist".pabu ]; then
rm -rf "$userplist"
recover_plist
elif [ -d "$userpref".pabu ] ; then
rm -rf "$userpref"
recover_asf
fi
else
rm -rf "$userpref".pabu
rm -rf "$userplist".pabu
fi
fi
}

# ========================================================
# check_p_asf
# Check and create Portable Application Support folders
# ========================================================

function check_p_asf {
if [ ! -d "$p_userpref" ]; then
mkdir -p "$p_userpref"
fi
}

# ========================================================
# copy_local_pref
# Copy local preferences to Portable Application
# ========================================================

function check_asf {
if [ -d "$p_userpref" ]; then
rm -Rf "$p_userpref"
else
mkdir -p "$p_userpref"
fi
}

# copy Preferences Folder
function copy_pf {
userprefsize=`du -hc "$userpref" | cut -f1 | tail -1`
message4="Your $appid Preferences folder is $userprefsize. Copy it to Portable $appid?"
button=`/usr/bin/osascript << EOT
tell application "Finder"
beep
display dialog "$message4" buttons {"Copy", "Cancel"} \
with title "Copy $appid Preferences Folder" \
with icon note default button "Cancel"
set result to button returned of result
end tell
EOT`
if [ "$button" == "Copy" ]; then
check_asf
cp -R "$userpref" "$p_userpref"
touch -f "$copy_pref";
fi
}

function copy_local_pref {
if [ ! -f "$copy_pref" ] && [ -d "$userpref" ] ; then
message="Copy the existing $appid preferences on this system to Portable $appid?"
button=`/usr/bin/osascript << EOT
tell application "Finder"
beep
display dialog "$message" buttons {"Never", "Copy", "Don't Copy"} \
with title "Copy Preferences" \
with icon note default button "Don't Copy"
set result to button returned of result
end tell
EOT`
if [ "$button" == "Copy" ]; then
copy_pf
elif [ "$button" == "Never" ]; then
touch -f "$copy_pref";
fi
fi
}

# ========================================================
# bu/recover preference
# BackUp existing preference and restore portable ones
# ========================================================

function bu_plist {
mv "$userplist" "$userplist".pabu
}

function recover_plist {
# rm -rf "$userplist"
mv "$userplist.pabu" "$userplist"
}

# ========================================================
# bu/recover asf
# BackUp existing Application Support Folder and restore portable ones
# ========================================================

function bu_asf {
mv "$userpref" "$userpref".pabu
}

function recover_asf {
mv "$userpref".pabu "$userpref"
}

function recover_p_asf {
ln -s "$p_userpref" "$userpref"
}

function bu_p_asf {
rm -rf "$userpref"
}

# ========================================================
# run
# Open Portable Application
# ========================================================

function run_app {
here="`dirname \"$0\"`"
cd "$here"
"$here/app/$appid.app/Contents/MacOS/$appid"
}

# ========================================================
# open_p_app
# Run Portable Application script
# ========================================================

function open_p_app {
if [ -L "$userplist" ] ; then
bu_plist # Backup of existing preference
fi
if [ -d "$userpref" ] ; then
bu_asf # Backup existing Applicatin Support folder
fi
recover_p_asf # Recover portable Applicatin Support folder
run_app # Run application from external drive
bu_p_asf # Backup portable Applicatin Support folder
if [ -L "$userplist".pabu ] ; then
recover_plist # Restore existing preference
else # Remove portable preference
rm -rf "$userplist"
fi
if [ -d "$userpref".pabu ] ; then
recover_asf # Restore portable Application Support folder
fi
}

quitapp
repair
check_p_asf
copy_local_pref
open_p_app
.

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: