Jpp  master_rocky
the software that should make you happy
putFile.sh
Go to the documentation of this file.
1 #!/bin/zsh
2 #
3 # \author mdejong
4 #
5 script=${0##*/}
6 
7 source ${0%${script}}ulib.sh
8 
9 set_variable: DEBUG ARCHIVE_DEBUG 2
10 set_variable VERSION ""
11 
12 if do_usage $*; then
13  usage "$script <input file> <path> <detector identifier> <minimal run> <maximal run> <type> <version>"\
14  "\nUtility script to store file in archive."
15 fi
16 
17 case $# in
18  7) set_variable INPUT_FILE $argv[1];
19  set_variable DIR $argv[2];
20  let ID=" $argv[3]";
21  let MINRUN=" $argv[4]";
22  let MAXRUN=" $argv[5]";
23  set_variable TYPE $argv[6];
24  set_variable VERSION $argv[7];;
25  *) fatal "Wrong number of arguments."
26 esac
27 
28 if [[ ! -d $DIR ]]; then
29  fatal "Invalid path $argv[2]"
30 fi
31 
32 if [[ ! -f $INPUT_FILE ]]; then
33  fatal "File $INPUT_FILE does not exist."
34 fi
35 
36 if (( $ID == 0 )); then
37  fatal "Invalid detector identifier $argv[3]"
38 fi
39 
40 if (( $MINRUN == 0 )); then
41  fatal "Invalid minimal run $argv[4]"
42 fi
43 
44 if (( $MAXRUN == 0 )); then
45  fatal "Invalid minimal run $argv[5]"
46 fi
47 
48 if (( $MINRUN > $MAXRUN )); then
49  fatal "Invalid minimal / maximal run $argv[4] / $argv[5]"
50 fi
51 
52 if [[ $VERSION == *[^[:alnum:_-]]* ]]; then
53  fatal "Invalid version $argv[7] (special characters not allowed)"
54 fi
55 
56 setopt extendedglob
57 
58 typeset -Z8 ID
59 typeset -Z8 MINRUN
60 typeset -Z8 MAXRUN
61 
62 let COUNTER="0"
63 
64 if [[ -d $DIR/$ID/$TYPE ]]; then
65 
66  typeset -T BUFFER ARRAY /
67 
68  (ls -1 $DIR/$ID/$TYPE/_*/*/*/*) 2> /dev/null | while read BUFFER; do
69 
70  set_variable A_ID ${ARRAY[-6]}
71  set_variable A_TYPE ${ARRAY[-5]}
72  set_variable A_VERSION ${ARRAY[-4]#_}
73  let A_MINRUN=" ${ARRAY[-3]}"
74  let A_MAXRUN=" ${ARRAY[-2]}"
75  set_variable A_FILENAME ${ARRAY[-1]}
76 
77  let A_COUNTER="${A_FILENAME:r}"
78 
79  if (( $MINRUN <= $A_MAXRUN && $MAXRUN >= $A_MINRUN )); then
80  if (( $A_COUNTER > $COUNTER )); then
81  let COUNTER="$A_COUNTER"
82  fi
83  fi
84  done
85 fi
86 
87 let COUNTER="$COUNTER + 1"
88 
89 set_variable OUTPUT_FILE $DIR/$ID/$TYPE/_${VERSION}/$MINRUN/$MAXRUN/${COUNTER}.${INPUT_FILE:e}
90 
91 mkdir -p ${OUTPUT_FILE:h}
92 
93 if (( $? != 0 )); then
94  fatal "Path ${OUTPUT_FILE:h} not created."
95 fi
96 
97 cp -p $INPUT_FILE $OUTPUT_FILE
98 
99 if (( $? == 0 )); then
100  notice "Stored $INPUT_FILE to archive:$OUTPUT_FILE"
101 else
102  fatal "File $INPUT_FILE not stored in archive."
103 fi
104