~heber013/ubuntu-server-iso-testing/fixing-url-format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
# 
# Copyright (C) 2011, Canonical Ltd (http://www.canonical.com/)
#
# This file is part of ubuntu-iso-testing.
# 
# ubuntu-iso-testing 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 3 of 
# the License, or (at your option) any later version.
# 
# ubuntu-iso-testing 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 ubuntu-iso-testing.  If not, see 
# <http://www.gnu.org/licenses/>.
#
set -e

usage() {
	cat<<EOF
Usage: $(basename $0) [FILE]...
Extract files from an archive taking care of links

    -C directory  
            Change directories after opening the archive but before extracting
            entries from the archive.
    -f file 
            Read the archive from the specified file.
    -h      This help
EOF
	exit 1
}


DIRECTORY=""
ARCHIVE=""

TEMP=$(getopt -o hC:f:x -- "$@")
eval set -- "$TEMP"

while true ; do
    case "$1" in
        -h) 
            usage;;
        -C) 
            DIRECTORY=$2
            shift 2 ;;
        -f) 
            ARCHIVE=$2
            shift 2 ;;
        -x) # Does nothing just here for compatibility with bsdtar
            shift ;;
        --) shift ; break ;;
        *) usage;;
    esac
done

BSDTAROPTS=""

[ ! -z "$ARCHIVE" ] && BSDTAROPTS="$BSDTAROPTS -f $ARCHIVE"
#[ ! -z "$DIRECTORY" ] && BSDTAROPTS="$BSDTAROPTS -C $DIRECTORY"

BSDTARCMD="bsdtar -x $BSDTAROPTS"
LISTCMD="bsdtar -tv $BSDTAROPTS"

TEMPDIR=$(mktemp -d)
trap "rm -Rf $TEMPDIR" EXIT QUIT


for f in $@; do
    # Last component of list. This file always exists
    TARGETFILE=$( $LISTCMD $f|sed -e 's/.* //')
    [ -z "$TARGETFILE" ] && exit 1
    $BSDTARCMD -C $TEMPDIR $TARGETFILE
    
    [ "$(echo $f|cut -c1)" = "/" ] && f=".$f"

    TARGETDIR="."
    if [ ! -z "$DIRECTORY" ];then
        mkdir -p "$DIRECTORY"
        TARGETDIR="$DIRECTORY"
    fi
    mkdir -p $TARGETDIR/$(dirname $f)
    mv -f $TEMPDIR/$TARGETFILE $TARGETDIR/$f
done