#!/bin/sh

# tdiary-setup    setup your tdiary environment

TDIARY=/usr/share/tdiary
tdiary_conf=$TDIARY/tdiary.conf.sample

setup_symlink () {
    target=$1
    echo "make symlink of tdiary files to $target."
    [ -d $target ] || mkdir -m 755 -p $target
    ln -sf $TDIARY/index.rb $target/
    ln -sf $TDIARY/update.rb $target/
    ln -sf $TDIARY/misc/plugin/squeeze.rb $target/
}

setup_default () {
    target=$1
    echo "make default tdiary files to $target."
    [ -d $target ] || mkdir -m 755 -p $target
    install -m 701 $TDIARY/index-debian.rb $target/index.rb
    install -m 701 $TDIARY/update-debian.rb $target/update.rb
    install -m 701 $TDIARY/misc/plugin/squeeze.rb $target/squeeze.rb
}

setup_copy () {
    target=$1
    echo "make copy of tdiary files to $target."
    [ -d $target ] || mkdir -m 755 -p $target
    cp -dRf $TDIARY/* $target/
    cp -dRf $TDIARY/misc/plugin/squeeze.rb $target/squeeze.rb
}

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

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

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


    done=
    while [ -z "$done" ]; do
    echo "input data_path (default: $HOME/diary)"
    echo -n ": "
    read datapath
    [ "X$datapath" = "X" ] && datapath="$HOME/diary"
	
    echo
    echo "input smtp_host (default: localhost , or smtp.foo.net)"
    echo -n ": "
    read smtphost
    [ "X$smtphost" = "X" ] && smtphost="localhost"

    echo
    echo "choose English or Japanese [E/j]"
    echo -n ": "
    read language
    [ "X$language" = "X" ] && language="E"

    echo
    echo "data_path: $datapath"
    echo "smtp_host: $smtphost"
    echo "language:  $language"
    echo -n "Is this correct? [Y/n]"
    read yn
    if [ -z "$yn" -o "$yn" = "y" -o "$yn" = "Y" ]; then
       	done=y
    fi
    if [ -z "$language" -o "$language" = "E" -o "$language" = "e" ]; then
	tdiary_conf=$TDIARY/misc/i18n/tdiary.conf.sample-en
    elif [ "$language" = "J" -o "$language" = "j" ]; then
	tdiary_conf=$TDIARY/tdiary.conf.sample
    else
	done=
	language=
    fi
    done
    echo
    (
	cd $target
	sed -e "/^@data_path/s|^.*$|@data_path = '$datapath'|;/^@smtp_host/s|^.*$|@smtp_host = '$smtphost'|;/^@css/s|^.*$|@css = '/tdiary/theme/default.css'|;/^@cache_path/s|^.*$|@cache_path = '/tmp/$USER'|" $tdiary_conf > tdiary.conf
        echo -e "@options['sp.path'] = '/usr/share/tdiary/misc/plugin'"  >> tdiary.conf
    )

    if [ ! -d $datapath ]; then
	case "$option" in
	    default)
		mkdir -m 701 $datapath
		mkdir -m 701 $target/images
		;;
	    *)
		mkdir -m 777 $datapath
		mkdir -m 777 $target/images
		;;
	esac
    else
	echo "Delete data cache."
	rm -rf $datapath/cache
    fi    

}

update () {
    target=$1

    if [ -h $target/index.rb ]; then
	setup_symlink $target
	return $?
    elif [ -f $target/index.rb ]; then
	lines=`wc -l $target/index.rb | awk '{print $1}'`
        if [ $lines -ge 10 ]; then
	    setup_copy $target;
	    return $?
        else
	    setup_default $target;
	    return $?
	fi
    else
	echo "Update is failure"
	return 1
    fi
}

usage () {
    cat <<EOF 
Usage: $0 (default/symlink/copy/update) directory
ex) $0 default /home/$USER/public_html/diary

directory is where CGI files of tDiary will be put. It should be absolute path.

If you set up tDiary at first, choose default, symlink, copy option.
  Choose default if your httpd runs under suEXEC mode.
  Choose symlink or copy otherwise.

If you set up existing tDiary again, choose update option.
EOF
}


RETVAL=0

# See how we were called.

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

case "$1" in
    default)
        echo "Setup tDiary as default setting: "
        setup_default $2
	setup_main $1 $2
        RETVAL=$?
        ;;
    symlink)
        echo "Setup tDiary by symbolic link: "
        setup_symlink $2
	setup_main $1 $2
        RETVAL=$?
        ;;
    copy)
        echo "Setup tDiary by copy: "
        setup_copy $2
	setup_main $1 $2
        RETVAL=$?
        ;;
    update)
        echo "Update tDiary: "
        update $2
        RETVAL=$?
        ;;
    *)
        usage
        exit 1
esac

exit $RETVAL
