-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract-from-isa
executable file
·130 lines (104 loc) · 3.64 KB
/
extract-from-isa
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# vi: fdm=marker
# Constants {{{1
################################################################
PROG_NAME=$(basename $0)
PROG_PATH=$(dirname $0)
YES=yes
NO=no
# Global variables {{{1
################################################################
DEBUG=0
EXT=
INPUT_DIR=
OUTPUT_DIR=
SYMLINK=$NO
# Print help {{{1
################################################################
function print_help {
echo "Usage: $PROG_NAME -i <ISA_DIR> -e <EXT> -o <OUTPUT_DIR>"
echo
echo "Extract files with a given extension from ISA-Tab archives into a collection."
echo
echo "Options:"
echo " -e, --ext EXT The extension of the files to find."
echo " -h, --help Print this help message."
echo " -i, --input DIR Input directory containing ISA archive."
echo " -o, --output DIR Set the output directory to use."
echo " -s, --symlink Create symbolic links instead of copying files."
}
# Error {{{1
################################################################
function error {
local msg=$1
echo "ERROR: $msg" >&2
exit 1
}
# Print debug msg {{{1
################################################################
function print_debug_msg {
local dbglvl=$1
local dbgmsg=$2
[ $DEBUG -ge $dbglvl ] && echo "[DEBUG] $dbgmsg" >&2
}
# Read args {{{1
################################################################
function read_args {
local args="$*" # save arguments for debugging purpose
# Read options
while true ; do
shift_count=1
case $1 in
-e|--ext) EXT="$2" ; shift_count=2 ;;
-g|--debug) DEBUG=$((DEBUG + 1)) ;;
-h|--help) print_help ; exit 0 ;;
-i|--input) INPUT_DIR="$2" ; shift_count=2 ;;
-o|--output) OUTPUT_DIR="$2" ; shift_count=2 ;;
-s|--symlink) SYMLINK=$YES ;;
-) error "Illegal option $1." ;;
--) error "Illegal option $1." ;;
--*) error "Illegal option $1." ;;
-?) error "Unknown option $1." ;;
-[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;;
*) break
esac
shift $shift_count
done
shift $((OPTIND - 1))
# Debug
print_debug_msg 1 "Arguments are : $args"
# Check input params
[[ $# -eq 0 ]] || error "No remaining arguments are allowed."
[[ -n $INPUT_DIR ]] || error "You must specify an input directory, using -i option."
[[ -d $INPUT_DIR ]] || error "\"$INPUT_DIR\" is not a valid directory."
[[ -n $OUTPUT_DIR ]] || error "You must specify an output directory, using -o option."
[[ ! -e $OUTPUT_DIR ]] || error "\"$OUTPUT_DIR\" already exists."
[[ -n $EXT ]] || error "You must specify the extension of the files you are looking for, with the -e option."
}
# MAIN {{{1
################################################################
read_args "$@"
# Create output directory
print_debug_msg 1 "Create output directory \"$OUTPUT_DIR\"."
mkdir -p "$OUTPUT_DIR"
# Find files to extract
print_debug_msg 1 "Find \"$EXT\" files to extract in \"$INPUT_DIR\"."
files_to_extract=$(mktemp -t tmp.XXXXXX)
find "$(realpath $INPUT_DIR)" -iname "*.$EXT" >files_to_extract
print_debug_msg 1 "Files to extract:"
if [[ $DEBUG -ge 1 ]] ; then
cat files_to_extract >&2
fi
# Extract files
if [[ $SYMLINK == $YES ]] ; then
print_debug_msg 1 "Create symbolic links of all \"$EXT\" files to extract into \"$OUTPUT_DIR\"."
xargs -I % ln -s % "$OUTPUT_DIR" <files_to_extract
else
print_debug_msg 1 "Copy all \"$EXT\" files to extract to \"$OUTPUT_DIR\"."
xargs -I % cp % "$OUTPUT_DIR" <files_to_extract
fi
rm files_to_extract
print_debug_msg 1 "Files extracted:"
if [[ $DEBUG -ge 1 ]] ; then
ls -1 "$OUTPUT_DIR" >&2
fi