#!/bin/sh -e

# This file is maintained at http://git.mdcc.cx/draai

# First version written at 2000-09-12.

# Copyright: © 2000-2007 Joost van Baal <joostvb-draai@mdcc.cx>
#
# This program is part of draai.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.  This program is distributed WITHOUT ANY WARRANTY. You should have
# received a copy of the GNU General Public License along with draai.  If not,
# see <http://www.gnu.org/licenses/>.


# when dirtree grows too big, triggers bug in dash:

# Date: Sat, 21 Dec 2002 18:30:44 +0100
# From: Joost van Baal <joostvb-debian-bugs@mdcc.cx>
# To: Debian Bug Tracking System <submit@bugs.debian.org>
# Subject: dash: cd builtin chokes on multiple repeated invocations   
# Message-ID: <20021221173043.GA23212@gelfand.mdcc.cx>

# bug number 173884

# ROOTS should be relative to $MP3HOME
#
# ROOTS="../../../../nfs/hille/home/dj/mp3 ../../../../nfs/schilow/var/mp3 ../../../../nfs/schilow/home/dj/mp3 ../../../../nfs/abramowitz/data1/8gieg/dj/mp3"
# MP3HOME=$HOME/mp3
# DEBUG=yes

rcfile=$HOME/.mp3symlinksrc
# this should define ROOTS and MP3HOME, and possibly DEBUG

if test -f $rcfile
then
    . $rcfile
else
    echo >&2 panic: no rcfile. it should set ROOTS and MP3HOME.
    exit 1
fi

test -n "$DEBUG" && set -x

echo >&2 cd $MP3HOME
cd $MP3HOME
# we are in ~/mp3 now, typically

for r in $ROOTS
do
    echo >&2 "--- $r ---"
    for a in $r/*
    do
        # a is ../blah/Vladislav_Delay
	a=`basename $a`
        echo >&2 "  -- $a"
	if test ! -d "$MP3HOME/$a"
	then
            # a is Vladislav_Delay
	    echo >&2 mkdir "$a"
	    mkdir "$a"
	fi

	test -n "$DEBUG" && pwd

	cd "$a"
        # we are in ~/mp3/Vladislav_Delay
	# clean up this artist's dir
	for A in *
	do
	    A="`basename $A`"
	    if test ! -h "$A"
	    then
		if test -e "$A"
		then
                    echo >&2 PANIC: a non-symlink $A exists in $PWD
                    exit 1
		fi
	    elif test ! -d "$A"
	    then
                echo >&2 a symlink $A which does not point to a dir in $PWD, skipping
            fi
	done

        # we are in ~/mp3/Vladislav_Delay
	# add stuff to this artists dir
	for A in $MP3HOME/$r/$a/*
	do
	    A=`basename $A`

            # A is Electric_Ladyland
	    if test ! -h "$A"
	    then
		if test -e "$A"
		then
		    echo >&2 PANIC: a non-symlink $A exists in $PWD
		    exit 1
		fi

                # one deeper : $r is relative to MP3HOME
                if test -d ../$r/$a/$A
                then
		    echo >&2 ln -s ../$r/$a/$A
		    ln -s "../$r/$a/$A"
                else
                    echo >&2 from $PWD, ../$r/$a/$A is not a directory, skipping
                fi
	    else
		if test ! -d "$A"
		then
		    echo >&2 PANIC: a symlink $A which does not point
		    echo >&2 to a dir in $PWD
		    exit 1
		fi
	    fi
	done

        cd $MP3HOME
    done
done

