#!/bin/bash
# -*- mode: sh; tab-width: 8; encoding: utf-8-unix -*-

prog=$( basename $0 .bash )
. /usr/local/bin/usr_local_tput.bash
ROLE=testforge

shopt -s nullglob || exit 1

. /usr/local/bin/usr_local_tput.bash
[ -f /etc/conf.d/hostname ] && . /etc/conf.d/hostname

if [ -n "$hostname" -a "$hostname" = "pentoo" ] ; then
    TO=/mnt/o/Cache/linuxPen19/var/cache/portage/packages
  elif [ -f /etc/dracut.conf.d/funtoo.conf ] ; then
    TO=/mnt/o/Cache/linuxFun64/var/cache/portage/packages
  elif [ -f /etc/dracut.conf.d/clipos.conf ] ; then
    TO=/mnt/o/Cache/linuxClipos/var/cache/portage/packages
  else
    exit 3
fi

[ -n "$TO" ] || { echo ERROR: empty TO ; exit 4 ; }
[ -d $TO ] || { echo ERROR: mkdir -p $TO ; exit 5 ; }

[ "$#" -eq 0 ] && set -- /usr/portage/packages

for root in $* ; do
    cd $root  || exit 2$?
    INFO $root
    find -L $root -type l -delete &

    pushd $TO
    #  -o -name \*.xpak
    find *-* virtual pentoo -type f -name \*tbz2 | grep -v '_sources\|_download_' | while read file ; do
	[ -h $root/$file ] && [ -f $file ] && continue
	dir=`dirname  $root/$file`
	[ -d $dir ] || mkdir $dir
	ln -s $TO/$file $root/$file
      done
    popd
  done
exit 0
