#!/bin/bash

set -e


help()
{
    echo "Usage: $0 [OPTIONS] [-m module1,module2]"
    echo "
    -P, --project_name    Specify PROJECT name
    -k, --kitname         Specify kit name
    -I, --integration     Specify Set the structure of the source
    -d, --date            Specify set date
    -D, --dep11           Specify set dep11 dir
    -t,--task             Specify task
    -c, --cpu             Specify CPU archive
    -w, --workspace       Specify WORKSPACE
    "
}

parse_args()
{
    short_options="P:k:D:c:d:I:t:w:h::"
    long_options="project:,kitname:,dep11:,cpu:,date:,integration:,task:,workspace:,help::"
    if getopt -o $short_options -l $long_options -n "ERROR" -- "$@" >/dev/null; then
        opts=$(getopt -o $short_options -l $long_options -n "ERROR" -- "$@")
        eval set -- "$opts"
    else
        help
        exit 1
    fi

   while [ $# -gt 0 ]
    do
        case "$1" in
            -k | --kitname)
                kitname=$2
                shift
                ;;
            -P | --project)
                PROJECT=$2
                shift
                ;;
            -c | --cpu)
                cpu=$2
                shift
                ;;
            -d | --date)
                date=$2
                shift
                ;;
            -t | --task)
                task=$2
                shift
                ;;
            -I | --integration)
                integration=$2
                shift
                ;;
            -w | --workspace)
                WORKSPACE=$2
                shift
                ;;
            -h | --help)
                help
                exit 0
                ;;
            ?)
                echo "unkonw argument $1"
                help
                exit 1
                ;;
        esac
        shift
    done
}

parse_args $@

CONFIG=${WORKSPACE}/dep11
data_dir=$CONFIG/export/data/$kitname
METADATA_DIR=${HOME}/pool/repos/repos-maker-workspace/AppStream_metadata/"$kitname_$task_$date"
CPUS=$(echo $cpu | tr ',' ' ')

for architecture in $(echo $CPUS)
do
    if [ "$task" = 'live' ]; then
        UP_DIR="${date}-live-${architecture}"
    else
        UP_DIR="${date}-${architecture}"
    fi
    UP_SRC_DIR=${WORKSPACE}/$UP_DIR
    PATH_DIR=$UP_SRC_DIR
    if [ "$INTEGRATED" == "distributed" ]; then
        PATH_DIR="$UP_SRC_DIR$architecture"
    fi
    CODENAME=$PATH_DIR/dists/$kitname
    cat $CONFIG/asgen-config.json.in \
        | sed -e "s#@PATH@#$PATH_DIR#g" \
            -e "s#@PROJECT@#$PROJECT#g" \
            -e "s#@ARCH@#$architecture#g" \
            -e "s#@CODENAME@#$kitname#g" \
            > $CONFIG/asgen-config.json

    cd $CONFIG
    appstream-generator process ${kitname}
    cd -
    for metadata in $(find $data_dir -type f ! -name *@* -name *gz)
    do
        gunzip -c $metadata > ${metadata%.*}
    done
    for area in $(ls $data_dir)
    do
        mkdir -p $CODENAME/$area/dep11
        cp -rf $data_dir/$area/* $CODENAME/$area/dep11
    done
    mkdir -p $METADATA_DIR/$architecture
    mv $CONFIG/cache  $CONFIG/db $CONFIG/'export' $METADATA_DIR/$architecture
done
