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

import os
import re
import sys
import sunhpc
import shutil

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

class Command(command):
    """
    Compilation and installation of the parallel computing software.

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

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

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

    <param type="path" name="envs">
    Specifies the environment variable storage path. Default: /share/apps/envs
    </param>

    <example cmd='soft mpi prefix=/share/apps/soft/xxx version=x.x.x'>
    Build the command line and associated parameters for this software.
    </example>
    """
    def run(self, params, args):

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

        dirsname = os.path.join(source, 'hpcsoft', 'Gaussian')
        suffname = 'tar.bz2'
        verslist = []
        for ver in os.listdir(dirsname):
            if ver.endswith(suffname):
                verslist.append(ver.split('.')[0])

        # args: g09-a01
        if len(args):
            version = args[0].split('-')[1]

        if not version:
            self.msg('must supply an "Version Number", e.g,, version=%s' % '/'.join(verslist), 'a')

        softname = args[0].split('-')[0]
        

        self.create_dirs(prefix, source, envs, version, softname, suffname, dirsname)

        self.build(prefix, softname)
        self.clean()

    def build(self, prefix, softname):
        """Start build the software."""
        self.cwd = os.getcwd()

        names = os.listdir(self.tmpdirs)
        if self.basename not in names:
            self.msg('Unpack the file from the %s to the %s directory.' % (self.filename, prefix))
            os.system('tar -xf %s -C %s' % (self.source, self.prefix))


        with open(self.envfile, 'w') as f:
            f.write('#!/bin/sh\n')
            f.write('#\n# %s env config\n#\n\n' % self.basename)
            f.write('export %sroot=%s/%s\n' % (softname, self.prefix, self.basename))
            f.write('source $%sroot/%s/bsd/%s.profile\n' % (softname, softname, softname))
            f.write('export GAUSS_SCDIR=~/gstmp\n\n')
            f.write('[ -e ~/gstmp ] || mkdir -p ~/gstmp\n\n')

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

    def create_dirs(self, prefix, source, envs, version, softname, suffname, dirsname):
        """Create base build directory."""

        self.basename = '%s-%s' % (softname, version)
        self.filename = '%s.%s' % (self.basename, suffname)

        self.prefix = os.path.join(prefix, 'gaussian')
        if not os.path.exists(self.prefix):
            os.makedirs(self.prefix)

        self.source = os.path.join(dirsname, self.filename)
        if not os.path.exists(self.source):
            self.msg('The %s does not exist,please check it.' % self.source, 'a')

        if not os.path.exists(envs):
            os.makedirs(envs)
        self.envfile = os.path.join(envs, '%s-%s' % (softname, version))

        self.tmpdirs = os.path.join(prefix, 'tmpdirs', softname)
        if not os.path.exists(self.tmpdirs):
            os.makedirs(self.tmpdirs)

    def clean(self):
        """Clean the install tmp directory."""
        if os.path.exists(self.tmpdirs):
            shutil.rmtree(self.tmpdirs)



        



























