#
#coding:utf-8
#
#Author  : QCSun
#Email   : qcsun@sunhpc.com
#Times   : 2023-04-14 05:21:02
#WebSite : https://www.sunhpc.com

import os
import sys
import sunhpc
import shutil

class command(sunhpc.commands.soft.command):
    pass

class Command(command):
    """
    Build the Gaussian software.

    <arg type="string" name="version">
    Specifies the software version. e.g,, version=03/09/16
    </arg>

    <param type="string" name="ext">
    Only the Gaussian09 version will use it, e.g,, ext=a01/d01/e01.
    Default: E01
    </param>

    <param type="path" name="prefix">
    Specifies the software install path.
    Default: /share/apps/soft/gaussian
    </param>

    <param type="path" name="envs">
    Specifies the software env path.
    Default: /share/apps/envs
    </param>

    <param type="path" name="source">
    Specifies the software source path. e.g,, /mnt/usb
    Default: /mnt/usb
    </param>

    <example cmd='soft gaussian prefix=/share/apps/soft version=16'>
    install the gaussian software.
    </example>
    """
    def run(self, params, args):

        (prefix, version, source, envs, ext) = self.fillParams([
            ('prefix', '/share/apps/soft/gaussian'),
            ('version', None),
            ('source', '/mnt/usb'),
            ('envs', '/share/apps/envs'),
            ('ext', 'e01'),
            ])

        if len(args):
            version = args[0]

        if not version:
            self.msg('must supply an "Gaussian version" e.g,, version=03/09/16', 'a')

        try:
            os.makedirs(prefix)
            self.msg("The %s directory does not exist,and it will be created." % prefix, 'w')
        except FileExistsError:
            pass

        if not os.path.exists(envs):
            os.makedirs(envs)

        if version == '03':
            if ext in ['d01']:
                basename = 'g03-d01'
            else:
                basename = 'g03-std'

        elif version == '09':
            basename = 'g09-%s' % ext.lower()

        elif version == '16':
            basename = 'g16-a03'

        else:
            self.msg('version error, must is 03/09/16', 'a')

        filename = '%s.tar.bz2' % basename


        pathname = 'Gaussian%s' % version
        softname = os.path.join(source, 'hpcsoft/Gaussian', pathname, filename)

        if not os.path.exists(softname):
            self.msg('The "%s" not found.' % softname, 'a')

        gaussian = os.path.join(prefix, basename)


        softlist = os.listdir(prefix)
        print ('------softlist-----', softlist)
        print ('------basename-----', basename)
        if basename in softlist:
            self.msg('The %s already exists,to reinstall it, remove it first.' % gaussian)
        else:
            self.msg('Start installing the %s software to the %s directory...' % (basename, prefix))
            os.system('tar -xf %s -C %s' % (softname, prefix))

        gsenv = os.path.join(envs, '%s-env.sh' % basename)
        with open(gsenv, 'w') as f:
            f.write('#!/bin/sh\n')
            f.write('#\n# %s env config\n#\n\n' % gaussian)
            f.write('export g%sroot=%s\n' % (version, prefix))
            f.write('source $g%sroot/g%s/bsd/g%s.profile\n\n' % (version, version, version))
            f.write('export GAUSS_SCDIR=~/gstmp\n')

        # create shared user and group.
        self.msg('Create a shared group to run the %s software.' % softname) 
        self.msg('  1, groupadd -g 888    public ')
        self.msg('  2, usermod  -G public dell   ')
        self.msg('  3, chown    -R root:public %s/%s' % (prefix, basename))
        self.msg('')
        self.msg('  source %s' % gsenv)




























