#!/bin/bash

# (c) 2008 by Florian Knodt
#Tested only on gentoo without vhost-USE-Flag

#The system/user can change from dialog to e.g. Xdialog
DIALOG=${DIALOG=dialog}

if [ ! -d /var/svn/ ] ;then
    $DIALOG --backtitle "Repmin SVN Repository administration" --title "Fehler" --msgbox "SVN-Verzeichnis nicht gefunden!" 0 0
    exit 1
fi

while : ; do
    #Hauptmenue
    temp=`mktemp`
    $DIALOG --backtitle "Repmin SVN Repository administration" --no-cancel --title "Hauptmenue" --menu "Bitte Aktion waehlen" 0 0 0 1 "Neues Repository" 2 "Repository loeschen" 3 "Beenden" 2>$temp
    action=`cat $temp`
    rm $temp
    case $action in
        '1')
            temp=`mktemp`
            $DIALOG --backtitle "Repmin SVN Repository administration" --title "Neues Repository"  --inputbox "Name des Repository" 0 0 2> $temp
            if [ $? -eq 0 ] ;then
                rep=`cat $temp`

                if [ -d /var/svn/$rep ] || [ -z $rep ] ;then
                    $DIALOG --backtitle "Repmin SVN Repository administration" --title "Fehler" --msgbox "Repository $rep existiert bereits" 0 0
                else 

                    $DIALOG --backtitle "Repmin SVN Repository administration" --title "Neues Repository"  --yesno "WebSVN-Eintrag erstellen?" 0 0
                    websvn=$?

                    COUNT=0
                    STEPS=6
                    if [ $websvn -eq 0 ] ;then
                        STEPS=`expr $STEPS + 1`
                    fi

                    STEP_P=`expr 100 / $STEPS`

                    (
                      
                        echo $COUNT
                        echo "XXX"
                        echo "Erstelle Repository"
                        echo "XXX"
                        svnadmin create /var/svn/$rep
                        COUNT=`expr $COUNT + $STEP_P`


                        echo $COUNT
                        echo "XXX"
                        echo "Erstelle Basisstrukturen"
                        echo "XXX"
                        TDIR=`mktemp -d`
                        cd $TDIR
                        mkdir -p {trunk,tags,branches}
                        COUNT=`expr $COUNT + $STEP_P`

                        echo $COUNT
                        echo "XXX"
                        echo "Importiere erste Revision"
                        echo "XXX"
                        svn import -q -m "Initial Import" --non-interactive $TDIR file:///var/svn/$rep
                        COUNT=`expr $COUNT + $STEP_P`

                        echo $COUNT
                        echo "XXX"
                        echo "Rechte werden angepasst"
                        echo "XXX"
                        chown -R apache /var/svn/$rep
                        COUNT=`expr $COUNT + $STEP_P`

                        if [ $websvn -eq 0 ] ;then
                            grep "USE" /etc/make.conf | grep "vhosts"
                            if [ $? -eq 1 ] && [ -f /var/www/localhost/htdocs/websvn/include/config.php ] ;then
                                echo $COUNT
                                echo "XXX"
                                echo "WebSVN-Eintrag wird angelegt"
                                echo "XXX"
                                cp /var/www/localhost/htdocs/websvn/include/config.php /var/www/localhost/htdocs/websvn/include/config.old.php
                                head -n -1 /var/www/localhost/htdocs/websvn/include/config.php > $TDIR/websvn.php
                                echo "\$config->addRepository(\"$rep\", \"file:///var/svn/$rep\");" >> $TDIR/websvn.php
                                echo "?>" >> $TDIR/websvn.php
                                mv $TDIR/websvn.php /var/www/localhost/htdocs/websvn/include/config.php
                                COUNT=`expr $COUNT + $STEP_P`
                            else
                                websvn=255
                            fi
                        fi

                        echo "$COUNT"
                        echo "XXX"
                        echo "Aufraeumen..."
                        echo "XXX"
                        rmdir $TDIR/{trunk,tags,branches}
                        rmdir $TDIR

                        echo "100"
                        echo "XXX"
                        echo "Fertig!"
                        echo "XXX"

                    ) |
                    $DIALOG --backtitle "Repmin SVN Repository administration" --title "Neues Repository" --gauge "Repository wird angelegt..." 20 70 0

                    if [ $websvn -eq 255 ] ;then
                        $DIALOG --backtitle "Repmin SVN Repository administration" --title "Fehler" -msgbox "WebSVN-Eintrag konnte nicht angelegt werden - verwenden sie USE=vhosts?" 0 0
                    fi

                    $DIALOG --backtitle "Repmin SVN Repository administration" --msgbox "Vorgang abgeschlossen" 0 0
                fi
            fi
            rm $temp;;
        '2')
            temp=`mktemp`
            dirs=""
            for i in `cd /var/svn && find * -maxdepth 0 -type d`; do
                dirs="$dirs $i $i"
            done

            $DIALOG --backtitle "Repmin SVN Repository administration" --title "Repository-Liste" --menu "Bitte zu loeschendes Repository waehlen!" 0 0 0 $dirs 2> $temp
            dialogchk=$?
            rep=`cat $temp`
            
            if [ $dialogchk -eq 0 ] ;then
            
                grep '/' $temp
                if [ $? -eq 0 ] ;then
                    $DIALOG --backtitle "Repmin SVN Repository administration" --title "Fehler" --msgbox "uhm... rm -r /var/svn/$rep scheint mir keine gute idee zu sein... Wenn sie sicher sind führen sie den Befehl bitte manuell aus" 0 0
                else
                    if [ ! -d /var/svn/$rep ] || [ -z $rep ] ;then
                        $DIALOG --backtitle "Repmin SVN Repository administration" --title "Fehler" --msgbox "Repository $rep konnte nicht gefunden werden" 0 0
                    else

                        rm -r /var/svn/$rep

                        if [ -f /var/www/localhost/htdocs/websvn/include/config.php ] ;then
                            cp /var/www/localhost/htdocs/websvn/include/config.php /var/www/localhost/htdocs/websvn/include/config.old.php
                            grep -v "addRepository(\"$rep\"" /var/www/localhost/htdocs/websvn/include/config.php > /var/www/localhost/htdocs/websvn/include/config.new.php
                            mv /var/www/localhost/htdocs/websvn/include/config.new.php /var/www/localhost/htdocs/websvn/include/config.php
                        fi

                        $DIALOG --backtitle "Repmin SVN Repository administration" --title "Vorgang erfolgreich" --msgbox "Repository wurde geloescht" 0 0
                    fi
                fi
            fi
            rm $temp;;
        '3')
            exit 0;;
    esac
done
