#!/bin/sh

# tdiary-setup    setup your tdiary environment

setup_symlink () {
    target=$1
    echo "make symlink of tdiary files to $target."

    [ -d $target ] || mkdir -p $target
    ln -sf `find /usr/share/tdiary -maxdepth 1|grep -v '/plugin$'` $target/
    mkdir -p $target/plugin
    ln -sf /usr/share/tdiary/plugin/* $target/plugin/
}

setup_symlink2 () {
    target=$1
    cd $target
    cp tdiary.conf /tmp/tdiary.conf.sed
    sed -e "/^#@plugin_path/s|^#||;/^@plugin_path/s|^.*$|@plugin_path = '$target/plugin'|" /tmp/tdiary.conf.sed > /tmp/tdiary.conf
    cp /tmp/tdiary.conf .

    eval `grep '@data_path' tdiary.conf | sed -e "s/[@ ]//g"`    
    if [ -e $data_path/tdiary.conf ]; then
      cd $data_path
    fi
    if ! grep -q "@plugin_path" tdiary.conf ; then
      echo 'You had better add'
      echo "@plugin_path = '$target/plugin'"
      echo "  to $data_path/tdiary.conf"
    fi
}

setup_copy () {
    target=$1

    echo "make copy of tdiary files to $target."

    [ -d $target ] || mkdir -p $target
    cp -dRf /usr/share/tdiary/* $target/
}

setup_main () {
    target=$1
    update=$2

    sed -e 's/#Options +*/Options +/g' -e "s/foo/$USER/g" $target/dot.htaccess > $target/.htaccess

    if [ -f $HOME/.htpasswd ] && grep -q "^$USER:" $HOME/.htpasswd ; then
	echo "$USER is exist in $HOME/.htpasswd"
    else
	echo "input password at $USER in $HOME/.htpasswd"
	htpasswd -c $HOME/.htpasswd  $USER
    fi

    if [ "$update" = "" ]; then
	done=
	while [ -z "$done" ]; do
	echo "input data_path (ex: /home/$USER/diary)"
	echo -n ": "
	read datapath
	if [ "$datapath" = "" ]; then
            datapath="/home/$USER/diary"
	fi

	echo
	echo "input smtp_host (ex: localhost, smtp.hogefuga.net)"
	echo -n ": "
	read smtphost
	if [ "$smtphost" = "" ]; then
            smtphost="localhost"
	fi

	echo
	echo "data_path: $datapath"
	echo "smtp_host: $smtphost"
	echo -n "Is it ok? [(O)k/(e)dit]"
	read ok
	if [ -z "$ok" -o "$ok" = "o" -o "$ok" = "O" ]; then
		done=y
	fi
	done
	echo
	(
	    cd $target
	    sed -e "/^@data_path/s|^.*$|@data_path = '$datapath'|;/^@smtp_host/s|^.*$|@smtp_host = '$smtphost'|" tdiary.conf.sample > tdiary.conf
	)

	[ ! -d $datapath ] && mkdir $datapath
	echo "NOTICE: Please fix permission of $datapath in accordance with httpd configuration (a.k.a suEXEC or not)"
    fi
}

update () {
    target=$1

    if [ -h $target/index.rb ]; then
	#symlink
	setup_symlink $target
	setup_symlink2 $target
	return $?
    elif [ -f $target/index.rb ]; then
	setup_copy $target
	return $?
    else
	echo "Update is failure"
	return 1
    fi
}

usage () {
    echo "Usage: $0 (symlink/copy/update) directory"
    echo "ex) $0 copy /home/$USER/public_html/tdiary"
}


RETVAL=0

# See how we were called.

if [ "$2" = "" ]; then
    usage
    exit
fi

case "$1" in
    symlink)
        echo "Setup tDiary by symbolic link: "
        setup_symlink $2
	setup_main $2
	setup_symlink2 $2
        RETVAL=$?
        ;;
    copy)
        echo "Setup tDiary by copy: "
        setup_copy $2
	setup_main $2
        RETVAL=$?
        ;;
    update)
        echo "Update tDiary: "
        update $2
        RETVAL=$?
        echo "Please remove cache in @data_path"
        ;;
    *)
        usage
        exit 1
esac

exit $RETVAL
