#!/bin/bash

# LXCF - LXC Facility
# Copyright (C) 2013-2014 FUJITSU LIMITED

# 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; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

if [ $# -ne 0 ] ; then
  echo "usage: ${0##*/}"
  exit 1
fi

umask 022

# create /opt dir
mkdir -p /opt/lxcf

# create /etc dir
mkdir -p /etc/lxcf/rsc

# generate a ssh key
if [ ! -f /root/.ssh/lxcf_rsa ] ; then
  /usr/lib64/lxcf/lxcf-keygen
fi
if [ ! -f /root/.ssh/config ] ; then
  echo 'StrictHostKeyChecking=no' >> /root/.ssh/config
  echo 'IdentityFile	/root/.ssh/lxcf_rsa' >> /root/.ssh/config
fi

if [ -f /root/.bash_profile ] ; then
  profile=/root/.bash_profile
elif [ -f /root/.bash_login ] ; then
  profile=/root/.bash_login
elif [ -f /root/.profile ] ; then
  profile=/root/.profile
else
  touch /root/.bash_profile
  profile=/root/.bash_profile
fi
if ! grep -q "LXCF283AX" $profile ; then
  [ -n "`tail -n1 $profile`" ] && echo >> $profile
  cat <<- "EOF" >> $profile
	# The key to LXCF is registered (LXCF283AX)
	if [ -f /root/.ssh/lxcf_rsa ] ; then
	  eval `ssh-agent` > /dev/null 2>&1
	  ssh-add /root/.ssh/lxcf_rsa > /dev/null 2>&1
	fi
	EOF
#  cat <<- "EOF" >> $profile
#	
#	if [ -x /usr/lib64/lxcf/sbin/update-alert \
#	    -a ! -f /etc/lxcf/separate ] ; then
#	  alias rpm="/usr/lib64/lxcf/sbin/update-alert rpm"
#	  alias yum="/usr/lib64/lxcf/sbin/update-alert yum"
#	fi
#	EOF
fi

if [ ! -f /root/.bash_logout ] ; then
  touch /root/.bash_logout
fi
if ! grep -q "LXCF283AX" /root/.bash_logout ; then
  [ -n "`tail -n1 /root/.bash_logout`" ] && echo >> /root/.bash_logout
  cat <<- "EOF" >> /root/.bash_logout
	# The key to LXCF is registered (LXCF283AX)
	if [ -f /root/.ssh/lxcf_rsa ] ; then
	  eval `ssh-agent -k` > /dev/null 2>&1
	fi
	EOF
fi

exit 0
