[BACK]Return to manage_pre CVS log [TXT][DIR] Up to [local] / palm / manage_pre

File: [local] / palm / manage_pre / manage_pre (download)

Revision 1.10, Mon Jan 24 21:36:09 2011 UTC (13 years, 3 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +7 -8 lines

get rid of useless variable
reorder case statment to be the same order as the rest of the script

#!/bin/sh
# $AFresh1: manage_pre,v 1.10 2011/01/24 21:36:09 andrew Exp $

# Copyright (c) 2010 Andrew Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
local _pre_id='Palm, Pre, 0000'
local _mount_point='/mnt/pre'
local _unison_config='palm_pre'

find_pre() {
    local _dev=`dmesg | grep 'Palm, Pre' | tail -1 | cut -d' ' -f 1`
    if [ X"$_dev" == X"" ]; then
        echo Pre not found! >&2
        exit 1
    fi

    local _id=`dmesg | grep "^$_dev at " | tail -1 | sed -e 's/.*<//' -e 's/>.*$//'`
    if [ X"$_id" != X"$_pre_id" ]; then
        echo "Pre [$_dev] not correct id [$_id]" >&2
        exit 2
    fi

    echo /dev/${_dev}i
}

is_mounted() {
    /sbin/mount | grep "^${_dev} "
}

eject() {
    local _mounted=`is_mounted $_dev`
    if [ X"" != X"$_mounted" ]; then
        /bin/sync
        $SUDO /sbin/umount $_dev && echo Unmounted Pre
    else
        echo Not mounted
    fi
    $SUDO /bin/eject  $_dev 2> /dev/null && echo Ejected Pre
    $SUDO rmdir $_mount_point 2> /dev/null
}

mount() {
    local _mounted=`is_mounted $_dev`
    if [ X"" != X"$_mounted" ]; then
        echo Already mounted: ${_mounted%% type*}
        return
    fi
    $SUDO mkdir -p $_mount_point
    $SUDO chgrp users $_mount_point
    $SUDO chmod g+w   $_mount_point

    $SUDO /sbin/mount $_dev $_mount_point 2> /dev/null
    local _status=$?

    if [ "$_status" != 0 ]; then
        echo -n "Waiting for $_dev to be ready "
        while [ "$_status" != 0 ]; do
            echo -n .
            sleep 1
            $SUDO /sbin/mount $_dev $_mount_point 2> /dev/null
            _status=$?
        done
        echo
    fi
    echo Mounted Pre on $_mount_point
}

synchronize() {
    local _mounted=`is_mounted $_dev`
    local _did_mount=""
    if [ X"" == X"$_mounted" ]; then
        mount || exit 255
        _did_mount=1
    fi

    echo Synchronizing Pre

    #if [ -e /usr/local/bin/rsync ]; then
    #   rsync -rv --delete --size-only --exclude '*.pid'
    #fi

    if [ -e /usr/local/bin/unison ]; then
        /usr/local/bin/unison $_unison_config -auto -batch -ui text
    fi

    if [ ! -z "$_did_mount" ]; then
        eject
    fi
}

local _action="$*"
case `basename $0` in
    mount*)
        _action=mount
        ;;
    sync*)
        _action=sync
        ;;
    eject*)
        _action=eject
        ;;
esac

if [ -z "$_action" ]; then
    echo "manage_pre mount|sync|eject"
    exit 255
fi

local _dev=`find_pre`
if [ X"${_dev}" == X"" -o ! -e $_dev ]; then
    echo "Unable to find Pre!"
    exit 3
fi

local _a
for _a in $_action; do
    case "$_a" in 
        mount)
            mount
            ;;
        sync)
            synchronize
            ;;
        eject)
            eject
            ;;
        *)
            echo "Unrecognized action [$_a]"
            ;;
    esac
done