#!/usr/bin/env python3
# setup commands for first terminal session

#During an installation or update, the hard-coded path to BIRCH is replaced
# with the BIRCH install directory by birchhome.py

# This script is a compromise. The potential problem comes in
# if a particular dot file doesn't exist. On most systems, there
# is no problem in having newuser create all possible dot files
# that a give shell might need we omit ksh for reasons I won't
# go into here. The problem I found was on a system where
# tcsh was the default, but the user was only given a .cshrc file.
# Normally that shouldn't be a problem. However on this particular
# system if you do have a .tcshrc file, you must also have a
# line like
#
# source /usr/local/etc/tcshrc
#
# in order for global settings to be read. Of course,
# newuser can't be expected to know that such a line
# is needed, so if you suddenly create a .tcshrc
# file with the BIRCH 'source' line in it, BIRCH
# will work, but the global tcshrc file won't be
# read. This is such a rare occurrence (I've only
# ever seen it on one system) that my solution is
# to document it in the FAQ.
#
# If you think about it, it is bad policy for the
# system administrator to require a specific line
# in each user's dot files to read the system-wide
# rc files. That's asking for trouble.

import sys, os, os.path, platform

# We have to add the path to vncsetup.py and linklauncher.py so that we
# can import them.
pathtoadd = os.path.join("/home/birch/BIRCH", "script")
sys.path.insert(0, pathtoadd)
import vncsetup, linklauncher

# append contents of a source file to a 'dot' file.
# The source file calls BIRCH setup commands each
# time the user logs in or a new shell is started.
def birchappend(source_file, dest_files):
    # read the lines from the source file
    src_file = open(source_file, 'r')
    lines = src_file.readlines()
    src_file.close()

    # append the lines to each destination file
    for path in dest_files:
        if os.path.exists(path):
            os.chmod(path, 0o700)
        dest_h = open(path, 'a+')
        dest_h.writelines(lines)
        dest_h.close()
        os.chmod(path, 0o700)

def newuser(BIRCH, PLATFORM):
    print ("getting home directory")
    HOME = os.path.expanduser('~')

    #.login file, used by C-shell family
    print ("Writing .login")
    FILELIST=[os.path.join(HOME, '.login')]
    birchappend(os.path.join(BIRCH, 'admin', 'add_to_login'), FILELIST)

    # C-shell family
    print ("Writing .cshrc & .tcshrc")
    FILELIST=[os.path.join(HOME, '.cshrc'), os.path.join(HOME, '.tcshrc')]
    birchappend(os.path.join(BIRCH, 'admin', 'add_to_cshrc'), FILELIST)

    # Bourne-shell family
    print ("Writing Bourne Shell .profile, .bash_profile, .zshrc, etc.")
    FILELIST=[os.path.join(HOME, '.profile'),
              os.path.join(HOME, '.bash_profile'),
              os.path.join(HOME, '.bash_login'),
              os.path.join(HOME, '.bashrc'),
              os.path.join(HOME, '.zshrc')]
    birchappend(os.path.join(BIRCH, 'admin', 'add_to_profile'), FILELIST)

    # Setup a .vnc directory for the user.
    # Nothing is done unless there is a $BIRCH/local/admin/xstartup file.
    print ("Setting up VNC")
    vncsetup.vncsetup(BIRCH)

    #Link the launchers to the desktop
    print('Creating launcher links')
    linklauncher.main(BIRCH, PLATFORM)

    print('Done - newuser.py!')
    print('Logout and login again so that the changes can take effect.')

if __name__=="__main__":
    PLATFORM = platform.system()
    BIRCH    = '/home/birch/BIRCH'

    newuser(BIRCH, PLATFORM)
