aboutsummaryrefslogtreecommitdiffstats
path: root/s3p/generate.sh
blob: d28da1e894173a459d99de0f57bc4f2ca3a8ff91 (plain)
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
#!/usr/bin/env bash

function print_usage_and_exit {
    [ -z "$1" ] || echo "Error: $1"
    echo "Usage: $0 <output_dir> <random_number>"
    echo "    - <output_dir>: directory where the generated csar file will be put into"
    echo "    - <random_number>: random number to choose which csar template to use"
    exit 1
}

[ "$#" -ne 2 ] && print_usage_and_exit
OUTPUT=$1
SEED=$2
[ -d $OUTPUT ] || mkdir -p $OUTPUT
[ -d $OUTPUT ] || print_usage_and_exit "$OUTPUT is not a valid directory"
[[ $SEED =~ ^[0-9]+$ ]] || print_usage_and_exit "$SEED is not a integer"


ROOT_DIR=`dirname $(readlink -f $0)`
TMP_DIR=$ROOT_DIR/csar_tmp

declare -A TEMPLATES
#fill templates 
for path in $ROOT_DIR/templates/*; do
    [ -d "$path" ] || continue # not directory, skip
    dirname="$(basename "${path}")"
    TEMPLATES[$dirname]=${path}
done
KEYS=(${!TEMPLATES[@]})

key=${KEYS[$(( 10#$SEED % ${#KEYS[@]} ))]}
src=${TEMPLATES[$key]}

rm -rf $TMP_DIR
cp -r -f $src $TMP_DIR
find $TMP_DIR -type f | xargs sed -i "s/##RANDOM_RESOURCE_NAME##/s3p_${SEED}_${key}/g"
cd $TMP_DIR
echo $key
echo $src
zip -r "$ROOT_DIR/s3p_${SEED}_${key}.csar" *

rm -f "$OUTPUT/s3p_${SEED}_${key}.csar"
mv -f "$ROOT_DIR/s3p_${SEED}_${key}.csar" $OUTPUT/

cd $ROOT_DIR
rm -rf $TMP_DIR