# Copyright 2014 Novacoin Community
#!/bin/sh

mkdir /var/lib/novacoin
chmod 700 /var/lib/novacoin
groupadd novacoin
useradd -g novacoin -d /var/lib/novacoin novacoin
chown novacoin:novacoin /var/lib/novacoin
mkdir /var/lib/novacoin/.novacoin

mkdir /etc/novacoin
touch /etc/novacoin/novacoin.conf
chmod 600 /etc/novacoin/novacoin.conf
chown novacoin:novacoin /etc/novacoin/novacoin.conf
ln -s /etc/novacoin/novacoin.conf /var/lib/novacoin/.novacoin/novacoin.conf

echo -e '# https://github.com/novacoin-project/novacoin/wiki/Sample-configuration\n' > /etc/novacoin/novacoin.conf

read -p 'Enter RPC user: ' rpcuser
echo 'rpcuser='$rpcuser >> /etc/novacoin/novacoin.conf

read -p 'Enter RPC password: ' rpcpassword
echo 'rpcpassword='$rpcpassword >> /etc/novacoin/novacoin.conf

read -p 'Enter RPC allow IP[192.168.0.*]: ' rpcallowip
if [ $rpcallowip ]; then
    echo 'rpcallowip='$rpcallowip >> /etc/novacoin/novacoin.conf
else
    echo '# rpcallowip=192.168.0.*' >> /etc/novacoin/novacoin.conf
fi

read -p 'Enter RPC allow port[8344]: ' rpcallowport
if [ $rpcallowport ]; then
    echo 'rpcallowport='$rpcallowport >> /etc/novacoin/novacoin.conf
else
    echo '# rpcallowport=8344' >> /etc/novacoin/novacoin.conf
fi

read -p 'Enter path to wallet.dat (ex. /home/user/.novacoin) or skip: ' walletpath
if [ $walletpath ]; then
    cp $walletpath/wallet.dat /var/lib/novacoin/.novacoin
    chmod 600 /var/lib/novacoin/.novacoin/wallet.dat
    chown novacoin:novacoin /var/lib/novacoin/.novacoin/wallet.dat
fi

wget https://novaco.in/files/novacoin_portage.tar.gz
tar zxf novacoin_portage.tar.gz
rm -f novacoin_portage.tar.gz

pkg-config systemd
if [ $? = 0 ] ; then
   cp net-p2p/novacoin/files/novacoind.service /lib/systemd/system
   ln -s /lib/systemd/system/novacoind.service /etc/systemd/system/multi-user.target.wants/novacoind.service
   if [ '`which novacoind`' != '/usr/bin/novacoind' ] ; then
      echo -e '\e[1;31m * Change path to \e[1;37mnovacoind\e[1;31m in \e[1;37m/lib/systemd/system/novacoind.service\e[1;31m script or copy or symlink to \e[1;37m/usr/bin\e[0m'
   else
      systemctl start novacoind.service
   fi
else
    cp net-p2p/novacoin/files/novacoin.confd /etc/conf.d/novacoind
    cp net-p2p/novacoin/files/novacoin.initd-r1 /etc/init.d/novacoind
    chmod +x /etc/init.d/novacoind
   if [ '`which novacoind`' != '/usr/bin/novacoind' ] ; then
      echo -e '\e[1;31m * Change path to \e[1;37mnovacoind\e[1;31m in \e[1;37m/etc/init.d/novacoind\e[1;31m script or copy or symlink to \e[1;37m/usr/bin\e[0m'
   else
      /etc/init.d/novacoind start
   fi
fi


read -p 'Install log rotate script? [y/N]: ' q
if [[ $q =~ ^(y|Y) ]] ; then
   cp net-p2p/novacoin/files/novacoind.logrotate /etc/logrotate.d/novacoind
fi

rm -rf net-p2p

