00001 #include "Sct/Version.h"
00002 #include "Sct/SctNames.h"
00003 #include <iostream>
00004 #include <fstream>
00005 #include <cstring>
00006 #include <sstream>
00007 #include <stdlib.h>
00008 using namespace Sct;
00009 using namespace std;
00010
00011
00012 void longVersion() {
00013 cout << "VersionString:" << VersionString << endl;
00014 cout << "MajorVersion: " << MajorVersion << " Minor Version: " << MinorVersion << " Modifier: " << VersionModifier << endl;
00015 cout << "Version: " << Version << endl;
00016 cout << "BuildSystem: " << BuildSystem << endl;
00017 cout << "BuildHost: " << BuildHost << endl;
00018 cout << "BuildDate: " << BuildDate << endl;
00019 cout << "RodDaq Version: " << RodDaqVersion << endl;
00020 cout << "CmtConfig: " << CmtConfig << endl;
00021 cout << "TdaqVersion : " << TdaqVersion << endl;
00022 cout << "RootVersion: " << RootVersion << endl;
00023 cout << "GccVersion: " << GccVersion << endl;
00024 }
00025
00026 void shortVersion() {
00027 cout << Version << VersionModifier << endl;
00028 }
00029
00030 void check_scratch_exists(){
00031 bool ok=true;
00032 string cmd1 = string (" test -d ")+ SctNames::getTempDir();
00033 if (system(cmd1.c_str())!=0){
00034 cerr << "SctRodDaqVersion: ERROR Command failed : " << cmd1 << endl;
00035 ok=false;
00036 }
00037 string cmd2 = string (" test -w ")+ SctNames::getTempDir();
00038 if (system(cmd2.c_str())!=0){
00039 cerr << "SctRodDaqVersion: ERROR Command failed : " << cmd2 << endl;
00040 ok=false;
00041 }
00042 if(!ok){
00043 cerr << " .. when testing for scratch directory. " << endl;
00044 cerr << "please check your environment variable: SCT_SCRATCH_DIR" << endl;
00045 exit(1);
00046 }
00047 }
00048
00049 void check_equal_command(string cmd, string check_output, string label){
00050 istringstream iss(check_output);
00051 iss>>check_output;
00052 ostringstream oss; oss << SctNames::getTempDir() << "/SctRodDaqVersion.XXXXXX";
00053 char tempfile_template[oss.str().size()];
00054 char* tempfile = tempfile_template;
00055 strcpy(tempfile, oss.str().c_str());
00056 mkstemp(tempfile);
00057
00058 string fullcmd=cmd;
00059 fullcmd += " > "; fullcmd += tempfile;
00060 if (system (fullcmd.c_str() ) !=0 ) {
00061 cerr << "Error executing command :" << cmd << endl;
00062 exit(1);
00063 }
00064
00065 string output; ifstream f(tempfile); f>>output;
00066 system( (string("rm -f ")+tempfile).c_str() );
00067
00068 if (output != check_output) {
00069 cerr << "ERROR: SctRodDaqVersion likely wrong version of external software \n The output of the command `" << cmd << "'"
00070 << " was : `" << output << "'" << endl
00071 << "... but I think I was built with " << label << " = `" << check_output<< "'" << endl;
00072 exit(1);
00073 }else{
00074 cout << "SctRodDaqVersion: Checked " << label << "\t=\t" << check_output << endl;
00075 }
00076 }
00077
00078 void checkVersion(){
00079 check_scratch_exists();
00080 check_equal_command ( string ("echo $CMTCONFIG"), CmtConfig, "CmtConfig ");
00081 check_equal_command ( string ("echo $CMTRELEASE"), TdaqVersion, "TdaqVersion");
00082 check_equal_command ( string ("root-config --version"), RootVersion, "RootVersion");
00083 check_equal_command ( string ("gcc -dumpversion"), GccVersion, "GccVersion ");
00084 exit(0);
00085 }
00086
00087 void helpMessage() {
00088 cout << "Prints out version information about the SctRodDaq system." << endl << endl;
00089 cout << "Usage: SctRodDaqVersion <options>" << endl;
00090 cout << "Possible options are: " << endl;
00091 cout << "\t-h, --help\t\tPrint this help message" << endl;
00092 cout << "\t-l, --long\t\tPrint out all version information" << endl;
00093 cout << "\t-s, --short\t\tPrint out short version information" << endl;
00094 cout << "\t-c, --check\t\tCheck versions" << endl;
00095 cout << endl << "If no option is given, the default is -s" << endl;
00096 }
00097
00098 void handleShortArg(char arg) {
00099 switch (arg) {
00100 case 'h':
00101 helpMessage();
00102 break;
00103 case 'l':
00104 longVersion();
00105 break;
00106 case 's':
00107 shortVersion();
00108 break;
00109 case 'c':
00110 checkVersion();
00111 default:
00112 cout << "Argument -" << arg << " not recognized" << endl;
00113 }
00114 }
00115
00116 void handleLongArg(const char* arg) {
00117 if (strcmp(arg, "--help") == 0) return handleShortArg('h');
00118 if (strcmp(arg, "--long") == 0) return handleShortArg('l');
00119 if (strcmp(arg, "--short") == 0) return handleShortArg('s');
00120 if (strcmp(arg, "--check") == 0) return handleShortArg('c');
00121 cout << "Argument " << arg << " not recognized" << endl;
00122 helpMessage();
00123 }
00124
00125 void handleArg(const char* arg) {
00126 if (arg[0] != '-') {
00127 cout << "Argument " << arg << " not recognized" << endl;
00128 helpMessage();
00129 return;
00130 }
00131 if (strlen(arg) == 2) handleShortArg(arg[1]);
00132 else handleLongArg(arg);
00133 }
00134
00135 int main(int argc, char** argv) {
00136
00137 switch (argc) {
00138 case 1:
00139 shortVersion();
00140 break;
00141 case 2:
00142 handleArg(argv[1]);
00143 break;
00144 default:
00145 helpMessage();
00146 }
00147 return 0;
00148 }