#!/bin/bash
#This script will create and synchronize a local package mirror with the repository (as defined below) on knoppmyth.net, it will also update the database.
#This script uses a shared account on knoppmyth.net.  DO NOT change the account name
# and dont' ask for the password, instead setup ssh keys.
#


if  [ -e /etc/makepkg.conf ]
then
    . /etc/makepkg.conf
else
    echo "couldn't find /etc/makepkg.conf"
fi
cwd=`dirname $0`
REMOTE_DIR=/mount/repository/repo
DOCROOT=/data/pkg_repo/$CARCH
#export UNISON="/root/.unison"
export UNISON="/data/pkg_repo/.unison"
cp -f /build_tools/bin/km-up.prf  $UNISON/km-up.prf
cp -f /build_tools/bin/km-down.prf  $UNISON/km-down.prf
function sync_dirs {
    REMOTE=$1
    LOCAL=$2

    echo "                     Updating $3                        "
    echo "________________________________________________________"

    #first bring down new changes from knoppmyth.net
    echo "--------"
    echo
    echo "Syncing the  packages with knoppmyth.net"
    echo
    echo "--------"
    unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE $LOCAL

    if [ ! $? = 0 ]
    then
        echo "#######################################################"
        echo "##             ERRORS OCCURED                        ##"
        echo "#######################################################"
        exit 1

    fi
    #update the local database
    update_db_repo.sh $LOCAL $3

    #push the database back
    echo "--------"
    echo
    echo "Pushing the pkg database to knoppmyth.net"
    echo
    echo "--------"
    unison km-up  -logfile /tmp/unison-up.log -auto -batch -numericids -force $LOCAL  $LOCAL ssh://reposync@knoppmyth.net/$REMOTE 
    #scp $LOCAL/*.db.tar.gz reposync@knoppmyth.net:$REMOTE/  

    echo "-------------------------------------------------------"
    echo "-----------         Finished with $3        -----------"
    echo "-------------------------------------------------------"
}


function source_sync () {
    src_repo=$1
    case $1 in 
        testing )
            echo "updating source packages"
            REMOTE_DIR=/mount/repository/repo/src_packages/core-testing
            LOCAL=/data/pkg_repo/src_packages/core-testing
            unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE_DIR $LOCAL
            
            REMOTE_DIR=/mount/repository/repo/src_packages/extra-testing
            LOCAL=/data/pkg_repo/src_packages/extra-testing
            unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE_DIR $LOCAL
            ;;

          release )
            echo "updating source packages"
            REMOTE_DIR=/mount/repository/repo/src_packages/core
            LOCAL=/data/pkg_repo/src_packages/core
            unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE_DIR $LOCAL
            
            REMOTE_DIR=/mount/repository/repo/src_packages/extra
            LOCAL=/data/pkg_repo/src_packages/extra
            unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE_DIR $LOCAL
            ;;
         chroot-devel )
            echo "updating source packages"
            REMOTE_DIR=/mount/repository/repo/src_packages/chroot-devel
            LOCAL=/data/pkg_repo/src_packages/chroot-devel
            unison km-down  -logfile /tmp/unison-down.log -auto -batch  -numericids ssh://reposync@knoppmyth.net/$REMOTE_DIR $LOCAL
            ;;



    *)  
            echo "need to know the source repository [testing|release|chroot-devel]"
            exit 1
        ;;  
esac
}


#---------------------------------------------------------------
if [ ! -d $UNISON ]
then
    mkdir $UNISON
fi

if [ ! -f $UNISON/km-down.prf ]
then
    cp -f $cwd/km-down.prf $UNISON/
fi

if [ ! -f $UNISON/km-up.prf ]
then
    cp -f $cwd/km-up.prf $UNISON/
fi





clear
case $1 in
    testing)
        #sync_dirs $REMOTE $LOCAL
        sync_dirs $REMOTE_DIR/$CARCH/core-testing $DOCROOT/core-testing  core-testing
        sync_dirs $REMOTE_DIR/$CARCH/extra-testing $DOCROOT/extra-testing extra-testing
        source_sync testing
        ;;

    release)
        sync_dirs $REMOTE_DIR/$CARCH/core $DOCROOT/core  core
        sync_dirs $REMOTE_DIR/$CARCH/extra $DOCROOT/extra extra
        source_sync release
        ;;

    chroot-devel)
        echo "will update chroot-devel"
        sync_dirs $REMOTE_DIR/$CARCH/chroot-devel $DOCROOT/chroot-devel  chroot-devel
        source_sync chroot-devel
        ;;

    source)
        if [ x = x$2 ]
        then
            echo "need to know the source repository [testing|release|chroot-devel]"
            exit 1
        fi
        source_sync $2
        ;;
    *)
        echo "invalid options"
        echo "kmsync.sh (testing|release|chroot-devel|source[testing|release|chroot-devel]) "
        echo
        echo "EX:  kmsync.sh testing  <= will sync the testing repo with km.net update the local database and resync with km.net"
        #echo "kmsync.sh testing oneway <= will syncthe testing repo with km.net update the local database"

        ;;
esac
echo "Finished."
# End