#!/bin/sh

# home-etc library: obtain user-decided configuration directory
#
# Copyright (C) 2003 Pawel Wilk <siefca@gnu.org>,
#
# This is free software; see the GNU Lesser General Public License version 2
# or later for copying conditions.  There is NO warranty.
#

REQ_PACKAGE="home-etc"

echo "home-etc - migrate your dotfiles! v1.0.1 (LGPL)"
echo
echo "This script will migrate known configuration files and directories"
echo "which supports HOME-ETC from the given directory to the directory"
echo "pointed out by the HOME_ETC variable."
echo

MYNAME="$0"
if [ `echo "$MYNAME" | cut -b1,2 ` = "./" ]; then
    MYNAME="`pwd`/`basename $MYNAME`"
fi
SHORTNAME=`basename $MYNAME`

if [ x"$HOME_ETC" = x"" ]; then
    echo "$SHORTNAME: cannot obtain HOME_ETC variable - set it up first!"
    exit 1
fi

if [ ! -d "$HOME_ETC" ]; then
    echo "$SHORTNAME: directory $HOME_ETC not found - creating one.."
    mkdir -p "$HOME_ETC" || exit 1
fi

line=`grep -n -e '-home-etc-mark-' $MYNAME | grep -v grep | cut -d':' -f1` 
if [ x"$line" = x ]; then
    echo "$SHORTNAME: mark-line not found, aboting!"
    exit 1
fi

total=`cat $MYNAME | wc -l`
line=$((total-line))

echo
echo -n "Enter the source directory or press ENTER [$HOME]: "
REPLY=""
read
if [ x"$REPLY" = x ]; then
    REPLY="$HOME"
fi
DESTINATION="$REPLY"

echo "Press ENTER to continue or CTRL+C to abort now"
read

TMPFILE=`mktemp -q $TMP/$SHORTNAME.XXXXXX`
if [ $? -ne 0 ]; then
    echo "$SHORTNAME: Cannot create temp file, exiting.."
    exit 1
fi
TMPFILES=`mktemp -q $TMP/$SHORTNAME.XXXXXX`
if [ $? -ne 0 ]; then
    echo "$SHORTNAME: Cannot create temp file, exiting.."
    exit 1
fi
TMPFILED=`mktemp -q $TMP/$SHORTNAME.XXXXXX`
if [ $? -ne 0 ]; then
    echo "$SHORTNAME: Cannot create temp file, exiting.."
    exit 1
fi

tail -n $line $MYNAME | cut -f1 | sort | uniq > $TMPFILE
echo "$SHORTNAME: checking whether it is possible to query RPM.."
rpm --quiet -q $REQ_PACKAGE && rpm -q --qf='%{name}\n' --whatrequires $REQ_PACKAGE 2>/dev/null | sort | uniq >$TMPFILES;
userpm=`head -1 $TMPFILES`

if [ x"$userpm" = x ]; then
    echo "$SHORTNAME: cannot use RPM, trust my instincts.."
    tail -n $line $MYNAME | sort | uniq | tr -s '\t' | cut -f2 > $TMPFILES
    userpm=no
else
    echo "$SHORTNAME: RPM allows user queries - comparing package names.."
    comm "$TMPFILE" "$TMPFILES" | tr '\t' ',' | grep "^,," | cut -b3- > $TMPFILED
    tail -n $line $MYNAME | sort | uniq > $TMPFILE
    echo -n > $TMPFILES
    cat $TMPFILE  | while read l
    do
	package=`echo "$l" | cut -f1`
	file=`echo "$l" | tr -s '\t' | cut -f2`
	grep -q "^$package" $TMPFILED  && echo "$file"  >> $TMPFILES
    done
    userpm=yes
fi

rm -f $TMPFILE $TMPFILED	
echo "$SHORTNAME: moving files.."
cat $TMPFILES  | while read l
do
    if [ -f "$HOME/$l" -a -r "$HOME/$l" ]; then
	mv -f "$HOME/$l" "$HOME_ETC/$l"
	echo "$SHORTNAME: $HOME/$l\t\t-> $HOME_ETC/$l"
    elif [ -d "$HOME/$l" -a -r "$HOME/$l" ]; then
	mv -fr "$HOME/$l" "$HOME_ETC/$l"
	echo "$SHORTNAME: $HOME/$l\t\t-> $HOME_ETC/$l (dir)"
    fi
done

rm -f $TMPFILES
echo "done!"

exit 0

########################
# package	filename
#
# -home-etc-mark-
mutt		.mutt
mutt		.muttrc
pine		.pine
