#!/bin/bash

DEST_DIR="$MY_REPO/cgcs-tis-repo"
ORIGIN="$DEST_DIR/origin"

CREATEREPO=$(which createrepo_c)
if [ $? -ne 0 ]; then
   CREATEREPO="createrepo"
fi


if [ ! -f $ORIGIN ]; then
   echo "ERROR: file not found '$ORIGIN'"
   exit -1
fi

ORIGIN_DIR=$(cat $MY_REPO/cgcs-tis-repo/origin | grep -v '^#' | head -n 1)

if [ ! -d $ORIGIN_DIR ]; then
   echo "ERROR: directory not found '$ORIGIN_DIR'"
   exit -1
fi

echo "ORIGIN_DIR=$ORIGIN_DIR"
for d in $(find $ORIGIN_DIR -type d | tail -n +2); do
   RELATIVE_DIR=$(echo $d | sed "s#^$ORIGIN_DIR/##")
   if [ -d $DEST_DIR/$RELATIVE_DIR ]; then
      rm -rf $DEST_DIR/$RELATIVE_DIR/*
   fi
   echo "mkdir -p $RELATIVE_DIR"
   mkdir -p $DEST_DIR/$RELATIVE_DIR
done

for d in $(find $ORIGIN_DIR -type d | tail -n +2); do
   for f in $(find $d -maxdepth 1 -type f); do
      RELATIVE_FILE=$(echo $f | sed "s#^$ORIGIN_DIR/##")
      if [ -e "$DEST_DIR/$RELATIVE_FILE" ]; then
         rm -f "$DEST_DIR/$RELATIVE_FILE"
      fi

      ln -s $f "$DEST_DIR/$RELATIVE_FILE"
   done
done

for d in `find -L $DEST_DIR -type d -name repodata`; do
   (cd $d/..
    rm -rf repodata
    if [ -f comps.xml ]; then
       $CREATEREPO -g comps.xml `pwd`
    else
       $CREATEREPO `pwd`
    fi
   )
done