#!/bin/bash

# script to sync tools repos to public download server (download.tizen.org)
# most of the time, it will be run as 'tools' user in otctools server

RSYNC_OPT="-avz --exclude=micbootstrap --exclude=tizen-downloader --exclude=lthor-tool --exclude=*repocache* --delete-delay"



usage()
{
    sed -e 's/^    //' <<EndUsage
    usage: publishrepo --baseurl <PUBLIC_URL> --renamerepo <REPO NAME> <OBS REPO PATH> <RSYNC DEST>
        publishrepo --baseurl <PUBLIC_URL> --renamerepo <REPO NAME> --release <OBS REPO PATH> <RSYNC DEST>
        publishrepo --symlink <NAME> <LINK_DEST_PATH> <RSYNC DEST>
EndUsage
    exit 1
}

extract_release_notes()
{
    repo_dir=$1
    dist=($(ls $repo_dir))
    cwd=$(pwd)
    rpm_list=$(find $repo_dir -name '*.rpm')
    temp_list=$(mktemp)

    for rpm in $rpm_list
    do
        release_notes_file=$(rpm -qpl $rpm|grep "/RELEASE_NOTES$")
        release_notes_name=$(echo $release_notes_file|sed 's#/usr/share/doc/packages/\([a-z0-9]*\)/RELEASE_NOTES#RELEASE_NOTES_\U\1#')
        [ -n "$release_notes_name" ] || continue
        grep $release_notes_file $temp_list && continue

        # keep in temp list
        echo $release_notes_file >> $temp_list

        tempdir=$(mktemp -d)
        cwd=$(pwd)
        cd $tempdir
        rpm2cpio < $rpm |cpio -idmv
        cd $cwd

        cp $tempdir/$release_notes_file $repo_dir/$release_notes_name.txt
        rm -r $tempdir
    done

    rm $temp_list
}

get_release_id()
{
    DEST_DIR=$1
    # Get release ID
    year_month=$(date +%y.%m)
    release_num=$(rsync --list-only $DEST_DIR |grep " $year_month.*$"|wc -l)

    if [ $release_num -eq 0 ]; then
        RELEASE_ID=$year_month
    else
        RELEASE_ID=$year_month.$release_num
    fi

}

update_repo_file()
{

    if [ -z "$BASEURL" ]; then
        return
    fi

    repo_path=$1
    old_working_dir=$(pwd)
    for dir in $(ls $repo_path)
    do
        cd $repo_path/$dir

        # skip if no repo file found
        [ -f *.repo ] || continue

        # rename the repo file
        if [ -n "$REPONAME" ]; then
            # if there're more than one repo file, only keep the last one
            for repofile in $(ls *.repo)
            do
                mv -f $repofile $REPONAME.repo
            done
            sed -i "s#^\[.*\]#\[$REPONAME\]#g" $REPONAME.repo
            sed -i "s#^name=.*#name=$REPONAME#g" $REPONAME.repo
        fi
        # update baseurl
        public_url=$BASEURL/$dir
        sed -i "s#^baseurl.*#baseurl=$public_url#g" *.repo
        sed -i "/^gpg/ d" *.repo
    done

    cd $old_working_dir
}

sync_remote()
{
    OBS_PATH=$1
    RSYNC_DEST=$2

    temparchivedir=$(mktemp -d)

    rsync $RSYNC_OPT $OBS_PATH/* $temparchivedir/

    # Update the repo file to public URL
    update_repo_file $temparchivedir/

    # Extrace release notes
    extract_release_notes $temparchivedir/

    rsync $RSYNC_OPT $temparchivedir/* $RSYNC_DEST

    rm -rf $temparchivedir
}

update_link()
{
    LINK_NAME=$1
    LINK_DEST=$2
    RSYNC_DEST=$3

    tempdir=$(mktemp -d)
    cd $tempdir
    ln -s $LINK_DEST $LINK_NAME
    rsync -l $LINK_NAME $RSYNC_DEST/
    rm -rf $tempdir
}


[ $# -lt 1 ] && usage

for i in $*
do
    case $1 in
        --release)
            [ $# -ne 3 ] && usage
            get_release_id $3
            if [ -n "$RELEASE_ID" ]; then
                [ -n "$BASEURL" ] && BASEURL=$BASEURL/$RELEASE_ID
                sync_remote $2 $3/$RELEASE_ID/
                exit 0
            else
                echo "NO release ID! "
                exit 1
            fi
            ;;
        --symlink)
            [ $# -ne 4 ] && usage
            update_link $2 $3 $4
            exit 0
            ;;
        --baseurl)
            BASEURL=$2
            shift 2
            ;;
        --renamerepo)
            REPONAME=$2
            shift 2
            ;;
        *)
            if [ $# -eq 2 ]; then
                sync_remote $1 $2
                exit 0
            else
                usage
            fi
            ;;
    esac
done
