Some fixes for karmic
[mapsector.git] / mapsector
index d3c2a72..df455ec 100755 (executable)
--- a/mapsector
+++ b/mapsector
@@ -1,10 +1,35 @@
 #!/bin/sh
+#
+# mapsector -- Map sector number to file name(s)
+#
+# Copyright (C) 2009,2010 Stefan Bund <stefan@j32.de>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 program; if not, write to the
+# Free Software Foundation, Inc.,
+# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
 
 ###########################################################################
 
 mappers=""
 scanners=""
 version="unknown"
+name="`basename "$0"`"
+
+# Don't use custom language since we parse the output of lots of standard
+# utilities
+unset LANG
 
 register_mapper()
 {
@@ -18,52 +43,63 @@ register_scanner()
 
 load()
 {
-    for lib in `ls "$1"/[0-9][0-9]_*.sh | sort`; do
-       source $lib
+    local X
+    local lib
+
+    X="`dirname "$0"`"
+    if [ "`basename "$X"`" == "bin" ]; then # `"
+        libdir="`dirname "$X"`/share/$name" #`"
+    fi
+    if [ -z "$libdir" -o ! -d "$libdir" ]; then
+        libdir="$X/lib"
+    fi
+
+    if [ ! -d "$libdir" ]; then
+        echo "! Library directory not found" 1>&2
+        exit 1
+    fi
+
+    for lib in `ls "$libdir"/[0-9][0-9]_*.sh | sort`; do
+        source $lib
     done
 }
 
 # Map device/sector through blockdevice mappings (e.g. lvm)
 
-run()
+map()
 {
     local found
-    foundm=1
+    found=1
     while [ -n "$found" ]; do
-       found=""
-       for name in "$@"; do
-           if detect_$name; then
-               found=1
-               if do_$name; then
-                   return
-               fi
-               break
-           fi
-       done
+        found=""
+        for name in $mappers; do
+            if detect_$name; then
+                found=1
+                if do_$name; then
+                    return
+                fi
+                break
+            fi
+        done
     done
 }
 
-###########################################################################
-
-unset LANG
-
-#### Find library directory and load library files
+scan()
+{
+    for name in $scanners; do
+        if detect_$name; then
+            if do_$name; then
+                return
+            fi
+        fi
+    done
+}
 
-name="`basename "$0"`"
-X="`dirname "$0"`"
-if [ "`basename "$X"`" == "bin" ]; then # `"
-    libdir="`dirname "$X"`/lib/$name" #`"
-fi
-if [ -z "$libdir" -o ! -d "$libdir" ]; then
-    libdir="$X/lib"
-fi
+###########################################################################
 
-if [ ! -d "$libdir" ]; then
-    echo "! Library directory not found" 1>&2
-    exit 1
-fi
+#### Load library modules
 
-load "$libdir"
+load
 
 #### Parse command line arguments
 
@@ -76,11 +112,11 @@ help=""
 
 while true; do
     case "$1" in
-       -h|--help) help="0"; shift ;;
-       --noscan) noscan="0"; shift ;;
-       -V|--version) echo "$name $version"; exit 0 ;;
-       --) shift; break ;;
-       *) echo "! internal error"; exit 1 ;;
+        -h|--help) help="0"; shift ;;
+        --noscan) noscan="0"; shift ;;
+        -V|--version) echo "$name $version"; exit 0 ;;
+        --) shift; break ;;
+        *) echo "! internal error"; exit 1 ;;
     esac
 done
 
@@ -88,7 +124,7 @@ if [ -n "$help" -o -z "$2" ]; then
     cat <<EOF
 Usage: $name [-h|--help] [--noscan] <device> <sector>"
 
-mapsector -- Map sector numbers to file name(s)
+mapsector -- Map sector number to file name(s)
 
 Given a device and a sector number, mapsector will try to find the
 file name(s) mapping to this sector. It will try to gather as much
@@ -98,7 +134,7 @@ mapsector currently has support for the following mapping schemes:
 
 EOF
     for mapper in $mappers; do
-       describe_$mapper
+        describe_$mapper
     done
     cat <<EOF
 
@@ -106,7 +142,7 @@ mapsector currently supports the following filesystems
 
 EOF
     for scanner in $scanners; do
-       describe_$scanner
+        describe_$scanner
     done
 cat <<EOF
 
@@ -123,7 +159,7 @@ mapsector will give you the mountpoint).
 if '--noscan' is given, the possibly lengthy (!!) filesystem scan for
 filenames is skipped.
 
--- mapsector Version $version
+-- mapsector $version
 EOF
     exit "${help:-1}"
 fi
@@ -134,7 +170,7 @@ sector="$2"
 echo "device $device"
 echo "sector $sector"
 
-run $mappers
+map
 if [ -z "$noscan" ]; then
-    run $scanners
+    scan
 fi