#!/bin/bash
############################################################
#                                                          #
# lunar - Lunar module management utility                  #
#                                                          #
############################################################
# sorcery is part of the sorcery spell management utility  #
# Copyright 2001 by Kyle Sallee                            #
############################################################
#                                                          #
# this WAS sorcery script of a source based Linux distro,  #
# calls Sorcerer GNU/Linux, or SGL. SGL is no longer       #
# available with GPL license. Since this script was taken  #
# before licensing scheme change, no legal problems I      #
# guess.                                                   #
#                                                          #
# the code is re-written for Lunar. The previous Copyright #
# notices are kept; just in case some code is left :=)     #
# Kagan Kongar <kongar@tsrsb.org.tr>, 20020519             #
#                                                          #
############################################################
#                                                          #
# Parts Copyrighted Jason Johnston  2002 under GPLv2       #
#                                                          #
# Parts Copyrighted Kagan Kongar 2002 under GPLv2          #
#                                                          #
############################################################


help()	{
  cat  <<  EOF
Lunar -- a menu-driven software package management utility

Example:        lunar
  This invokes the full-screen interface
  
Usage:		lunar [general options] [command [command parameters]]

General options:

-d  |  --debug                  Enables debug messages
-h  |  --help                   Displays this help text
-v  |  --verbose                Increases the level of message output

Commands:

prune                   Removes old sources and install/compile logs
renew                   Checks ver. against moonbase & recompiles if necessary
update                  Fetches latest moonbase and then does a "renew"
rebuild                 Recompiles all installed modules
optimize                Shortcut to the optimization menu
fix                     Check and fix all modules and internal state of lunar
nofix                   Check but do not fix modules and internal state
fixdepends              Check and fix the dependency database of lunar
resurrect  modulename(s)  Force modulename(s) to be unpacked from /var/cache
install    modulename(s)  Install a checklist of modules
remove     modulename(s)  Remove a checklist of modules
hold       modulename(s)  Place a hold on a checklist of modules
unhold     modulename(s)  Remove a hold on a checklist of modules
exile      modulename(s)  Remove a module and prevent it from being resurrected
unexile    modulename(s)  Allows a module to be compiled|resurrected again
EOF
  exit  1
}

show_modules()	{ 
  for  MODULE in `list_modules "$1"`;  do
    if  !  module_installed  $MODULE  &&
        !  module_held       $MODULE  &&
        !  module_exiled     $MODULE
    then
      SHORT="Short Description Unavailable"
      (
        run_details $MODULE
        echo  $MODULE
        echo  $VERSION
        echo  $SHORT
      )
    fi
  done
}


show_sections()  {
  for SECTION in $(list_sections);  do
    [ -z "$(list_modules $SECTION)" ] || {
      echo  $SECTION
      echo  "section"
    }
  done
}


select_section() {
  $DIALOG  --title "Section Selection Menu"  \
           --default-item  "$SECTION"        \
           --ok-label      "Select"          \
           --cancel-label  "Exit"            \
           --menu                            \
           ""                                \
           0 40 15                           \
           `show_sections`
}


add_pkgs()  { (
  while
    SECTION=`select_section`
  do            
    while
      MODULES=`show_modules $SECTION`  &&
       MODULE=`$DIALOG  --title "Select module to install please"  \
                       --item-help                               \
                       --menu                                    \
                       "$SECTION"                                \
                       0 60 10                                   \
                       $MODULES`
    do
      DESCRIPTION=`$MOONBASE/$SECTION/$MODULE/DETAILS`

      if  $DIALOG  --title      "Install $MODULE"  \
                   --yesno      "$DESCRIPTION"    \
                   0 0
      then
        push_install_queue  $MODULE
        $DIALOG  --msgbox  "$MODULE added to the install queue."  0 0
      else
        $DIALOG  --msgbox  "$MODULE will not be installed."  0 0
      fi
    done
  done
) }


show_file()	{
  if  [  -f  $1  ];  then
    if  [  "`file  -b  $1  |  cut  -d ' '  -f1`"  == "bzip2" ];  then
      bzcat  $1  |  view_file
    else
      $DIALOG  --textbox  $1  10  0
    fi
  else
    $DIALOG  --msgbox  "File not found."  0  0
    return  1
  fi
}


file_list() {
  LIST=`ls  $1`
  for  LINE  in  $LIST;  do
    echo  $LINE 
    echo  `file  -b  $1/$LINE  |  cut  -d ' ' -f1`
  done
}


file_menu() {
  FILE_LIST=`file_list $1`
  echo  $1/`$DIALOG  --title  "Please select a file."  \
                     --menu  "" 0 0 0                  \
                     $FILE_LIST`
}


show_installed_modules()  {
  for LINE in $(sort  $MODULE_STATUS_BACKUP) ; do
  (
     MODULE=`echo "$LINE" | cut -d : -f1`
     STATUS=`echo "$LINE" | cut -d : -f3`
    VERSION=`echo "$LINE" | cut -d : -f4`
       SIZE=`echo "$LINE" | cut -d : -f5`
	
    # TODO THIS IS BROKEN
    if module_installed $MODULE ; then
      SHORT="Short Description Unavailable"
      run_module_file $MODULE DETAILS &> /dev/null
      echo -e "$MODULE\n"
      echo -e "$VERSION,$SIZE\n"
      echo -e "\"$SHORT\"\n"
    fi
  )
  done
}


remove_pkgs()	{
  while
    if  [  -z  "$INSTALLED_MODULES"  ];  then
      echo  "Discovering installed modules..."
      INSTALLED_MODULES=$(show_installed_modules)
    fi

    MODULE=`$DIALOG --title "Select module to remove please." \
                    --item-help                               \
                    --ok-label      "Select"                  \
                    --cancel-label  "Exit"                    \
                    --menu  "" 0 60 10                        \
                    $INSTALLED_MODULES`
  do
    DESCRIPTION="Description unavailable"
    run_module_file $MODULE DETAILS

    if  $DIALOG  --title      "Removal $MODULE"  \
                 --yesno      "$DESCRIPTION"    \
                 0 0
    then
      push_remove_queue  $MODULE                                    &&
      $DIALOG  --msgbox  "$MODULE added to the remove queue."  0 0  ||
      $DIALOG  --msgbox  "$MODULE is not really installed?"    0 0
    fi
  done
}



make_checklist()  {
  for  MODULE in `list_modules "$1"`;  do
    SHORT="Short Description Unavailable"
    (
    run_details $MODULE
    STATUS="OFF"

    if    module_installed  $MODULE  ||
          module_held       $MODULE
    then  STATUS="on"
    else  STATUS="off"
    fi

    echo  $MODULE
    echo  $VERSION
    echo  $STATUS
    echo  $SHORT
    )
  done
}


process_section()  {
     KEEP_LIST=$2

  for  LINE in `list_modules "$1"`;  do
    if  echo  -e  "$KEEP_LIST"  |
        grep  -q  "^$LINE\$"
    then  push_install_queue  $LINE
    else  push_remove_queue   $LINE
    fi
  done
}


select_pkgs()  {
  SELECT_TITLE="Module Toggle Selection Menu"
  SELECT_HELP="[X]=install  [ ]=remove"

  while  SECTION=`select_section`;  do
    [ -z "$(list_modules $SECTION)" ] ||
    CHECKLIST=`make_checklist  $SECTION`

    if  OUTLIST=`$DIALOG  --title "$SELECT_TITLE"   \
                          --ok-label  "Commit"      \
                          --item-help               \
                          --separate-output         \
                          --checklist               \
                          "$SELECT_HELP  $SECTION"  \
                          0 0 10                    \
                          $CHECKLIST`
    then  process_section  "$SECTION"  "$OUTLIST"
    fi
  done
}


make_hold_checklist()  {
  for LINE in $(sort $MODULE_STATUS_BACKUP) ; do
  (
    MODULE=$(echo "$LINE" | cut -d: -f1)
    STATUS=$(echo "$LINE" | cut -d: -f3)
    VERSION=$(echo "$LINE" | cut -d: -f4)
    if [ "$STATUS" == "installed" ] || [ "$STATUS" == "held" ] ; then
      [ "$STATUS" == "held" ] && HELD="on" || HELD="off"
      SHORT="Short description unavailable"
      if run_details $MODULE &> /dev/null ; then
        echo $MODULE
        echo $VERSION
        echo $HELD
        echo $SHORT
      fi
    fi
  )
  done
}


hold_pkgs()  {
   CHECKLIST=`make_hold_checklist`
  HOLD_TITLE="Select modules to hold or unhold"
   HOLD_HELP="[X]=held  [ ]=installed"

  if  OUTLIST=`$DIALOG  --title "$HOLD_TITLE"  \
                        --item-help            \
                        --ok-label  "Commit"   \
                        --separate-output      \
                        --checklist            \
                        "$HOLD_HELP"           \
                        0 0 10                 \
                        $CHECKLIST`
  then
    rm  -f  $MODULE_STATUS

    for  LINE  in  `cat  $MODULE_STATUS_BACKUP`;  do
        MODULE=`echo  "$LINE"  |  cut  -d : -f1`
         DATE=`echo  "$LINE"  |  cut  -d : -f2`
       STATUS=`echo  "$LINE"  |  cut  -d : -f3`
      VERSION=`echo  "$LINE"  |  cut  -d : -f4`

      if    echo  -e  "$OUTLIST"  |  grep  -q  "^$MODULE\$";  then
        STATUS="held";
      else
        STATUS=${STATUS/held/installed}
      fi

      echo  "$MODULE:$DATE:$STATUS:$VERSION"  >>  $MODULE_STATUS
    done

    cp  $MODULE_STATUS  $MODULE_STATUS_BACKUP
  fi

}


module_menu() {
  while
    U_HELP="Current software hinders crackers' attempts to infiltrate your box"
    M_HELP="Install and remove multiple modules from a section using a single selection"
    A_HELP="Simple safe, verbose way of selecting modules for installation"
    R_HELP="Simple safe, verbose way of selecting modules for removal"
    B_HELP="Rebuild all installed modules"
    H_HELP="Held modules will not be upgraded until unheld or broken"
    E_HELP="Done managing modules"
     TITLE="Module Menu"
        OK="Select"
    CANCEL="Exit"

    COMMAND=`$DIALOG  --title "$TITLE"           \
                      --item-help                \
                      --ok-label      "$OK"      \
                      --cancel-label  "$CANCEL"  \
                      --menu  ""  0 0 0          \
                      "A"  "Add"      "$A_HELP"  \
                      "B"  "Rebuild"  "$B_HELP"  \
                      "H"  "Hold"     "$H_HELP"  \
                      "R"  "Remove"   "$R_HELP"  \
                      "S"  "Select"   "$M_HELP"  \
                      "U"  "Update"   "$U_HELP"`
  do
    case  $COMMAND in
      U)  update       ;;
      S)  select_pkgs  ;;
      A)  add_pkgs     ;;
      R)  remove_pkgs  ;;
      B)  rebuild      ;;
      H)  hold_pkgs    ;;
      E)  break        ;;
    esac
  done
}


grep_install_logs() {
  if  WHAT=`$DIALOG  --inputbox                                \
                    "Please enter full path and name of file"  \
                    0 0`
  then
    cd  $INSTALL_LOGS
    grep  "$WHAT\$"  *  | view_file
  fi
}


set_email()  {
  if  ADMIN=`$DIALOG  --ok-label  "Commit"         \
                         --inputbox                   \
                "Please enter the email address of the person or role account that should receive reports from this box."  \
                0 0  "$ADMIN"`
  then
    write_config "ADMIN=" "    ADMIN=$ADMIN"
  fi
}


set_delay()  {
  if  PROMPT_DELAY=`$DIALOG  --ok-label  "Commit"  \
                             --inputbox            \
                             "Please enter the time in seconds to wait for a response when prompted with a question."  \
                              0 0  "$PROMPT_DELAY"`
  then
    write_config "PROMPT_DELAY=" "PROMPT_DELAY=$PROMPT_DELAY"
  fi
}


queue_menu()  {
  while
    I_HELP="View the list of modules selected for installation"
    i_HELP="Edit the list of modules selected for installation"
    R_HELP="View the list of modules selected for removal"
    r_HELP="Edit the list of modules selected for removal"
    M_HELP="View the datafile of installed modules"
    m_HELP="Edit the datafile of installed modules"
	
    COMMAND=`$DIALOG  --title "Queue Menu"                           \
                      --ok-label      "Select"                       \
                      --cancel-label  "Exit"                         \
                      --default-item  $COMMAND                       \
                      --item-help                                    \
                      --menu                                         \
                      ""                                             \
                      0 40 6                                         \
                      "I"     "View     install   queue"   "$I_HELP" \
                      "i"     "Edit     install   queue"   "$i_HELP" \
                      "R"     "View     removal   queue"   "$R_HELP" \
                      "r"     "Edit     removal   queue"   "$r_HELP" \
                      "M"     "View     module    status"  "$M_HELP" \
                      "m"     "Edit     module    status"  "$m_HELP"`
  do
    case  $COMMAND in
      M)  show_file  $MODULE_STATUS         ;;
      m)  edit_file  $MODULE_STATUS
          cp         $MODULE_STATUS         \
                     $MODULE_STATUS_BACKUP  ;;
      I)  show_file  $INSTALL_QUEUE        ;;
      i)  edit_file  $INSTALL_QUEUE        ;;
      R)  show_file  $REMOVE_QUEUE         ;;
      r)  edit_file  $REMOVE_QUEUE         ;;
    esac
  done
}


maintenance_menu() {
  while
    F_HELP="Check and fix all modules and internal state of lunar"
    N_HELP="Check all modules but do not fix the internal state"
    D_HELP="Check and fix the dependency database of lunar"
    P_HELP="Prune old sources and install/compile logs"
    
    COMMAND=`$DIALOG  --title "Maintenance Menu"                         \
                      --ok-label      "Select"                           \
                      --cancel-label  "Exit"                             \
                      --default-item  $COMMAND                           \
                      --item-help                                        \
                      --menu                                             \
                      ""                                                 \
                      0 40 6                                             \
                      "F"     "Fix everything"                 "$F_HELP" \
                      "N"     "Check everything but don't fix" "$N_HELP" \
                      "D"     "Fix dependencies"               "$D_HELP" \
		      "P"     "Prune old sources and logs"     "$P_HELP"`
  do
    case $COMMAND in
     F)  lunar fix          ;;
     N)  lunar nofix        ;;
     D)  lunar fixdepends   ;;
     P)  lunar prune        ;;
    esac
    message "${MESSAGE_COLOR}Press enter to return...${DEFAULT_COLOR}"
    read
  done
}


log_menu()  {
  while
    C_HELP="View compile logs of previously installed software"
    G_HELP="Discover a file's origin"
    I_HELP="View logs of files previously installed"
    c_HELP="Remove a compile log"
    i_HELP="Edit a log of files previously installed"

    COMMAND=`$DIALOG  --title "Log Menu"                          \
                      --ok-label      "Select"                    \
                      --cancel-label  "Exit"                      \
                      --default-item  $COMMAND                    \
                      --item-help                                 \
                      --menu                                      \
                      ""                                          \
                      0 40 5                                      \
                      "G"     "Grep    install logs"  "$G_HELP"   \
                      "I"     "View    install log"   "$I_HELP"   \
                      "i"     "Edit    install log"   "$i_HELP"   \
                      "C"     "View    compile log"   "$C_HELP"   \
                      "c"     "Remove  compile log"   "$c_HELP"`
  do
    case  $COMMAND in
      G)  grep_install_logs                      ;;
      I)  show_file  `file_menu  $INSTALL_LOGS`  ;;
      i)  edit_file  `file_menu  $INSTALL_LOGS`  ;;
      C)  show_file  `file_menu  $COMPILE_LOGS`  ;;
      c)  rm -f      `file_menu  $COMPILE_LOGS`  ;;
    esac
  done
}


feature_menu()  {
  A_HELP="Create convenient auto-install scripts for common executables?"
  B_HELP="Administrate groups of Unix/Linux boxes?"
  C_HELP="Colorized messages on lin and lrm?"
  D_HELP="Use tmpfs for module installs?"
  E_HELP="Remove files when lrm?"
  F_HELP="Check for and repair broken programs after updating lunar?"
  G_HELP="Install the garbage documentation? (extras like README, LICENSE etc)"
  H_HELP="Automatically resurrect modules instead of compiling them?"
  I_HELP="Create archives of installed software?"
  K_HELP="Keep source code in /usr/src on good compiles? (gcc profiling needs it)"
  M_HELP="Email reports?"
  P_HELP="Preserve modified files or backup them up and overwrite with defaults?"
  R_HELP="Prompt to view reports?"
  S_HELP="Play audio with prompts?"
  T_HELP="Disallow lrm of modules that would cause terrible malfunctions?"
  U_HELP="Automatically remove old sources and install caches upon lunar update?"
  V_HELP="View compilation as it happens?"
  W_HELP="Verbose display of compilation process?"
  Z_HELP="Custom modules in zlocal override equally named ones (NOT RECOMMENDED) ?"

         ARCHIVE=${ARCHIVE:=on}
   AUTORESURRECT=${AUTORESURRECT:=on}
         AUTOFIX=${AUTOFIX:=on}
       AUTOPRUNE=${AUTOPRUNE:=off}
           CABAL=${CABAL:=off}
           COLOR=${COLOR:=on}
     KEEP_SOURCE=${KEEP_SOURCE:=off}      
    MAIL_REPORTS=${MAIL_REPORTS:=off}
    VIEW_REPORTS=${VIEW_REPORTS:=off}
        PRESERVE=${PRESERVE:=on}
           SOUND=${SOUND:=off}
         SUSTAIN=${SUSTAIN:on}
          VOYEUR=${VOYEUR:=on}
            REAP=${REAP:=on}
         GARBAGE=${GARBAGE:=on}
	 VERBOSE=${VERBOSE:off}

        if [ -z $TMPFS ]; then
          TMPFS=on;
        fi

  if  TOGGLES=`$DIALOG  --title "Feature Menu"  \
                        --no-cancel             \
                        --item-help             \
                        --separate-output       \
                        --checklist             \
                        ""                      \
                        0 40 14                 \
      "ARCHIVE"          "Toggle"  "$ARCHIVE"       "$I_HELP"   \
      "AUTORESURRECT"    "Toggle"  "$AUTORESURRECT" "$H_HELP"   \
      "AUTOFIX"          "Toggle"  "$AUTOFIX"       "$F_HELP"   \
      "AUTOPRUNE"        "Toggle"  "$AUTOPRUNE"     "$U_HELP"   \
      "CABAL"            "Toggle"  "$CABAL"         "$B_HELP"   \
      "COLOR"            "Toggle"  "$COLOR"         "$C_HELP"   \
      "KEEP_SOURCE"      "Toggle"  "$KEEP_SOURCE"   "$K_HELP"   \
      "GARBAGE"          "Toggle"  "$GARBAGE"       "$G_HELP"   \
      "MAIL_REPORTS"     "Toggle"  "$MAIL_REPORTS"  "$M_HELP"   \
      "PRESERVE"         "Toggle"  "$PRESERVE"      "$P_HELP"   \
      "SOUND"            "Toggle"  "$SOUND"         "$S_HELP"   \
      "SUSTAIN"          "Toggle"  "$SUSTAIN"       "$T_HELP"   \
      "VIEW_REPORTS"     "Toggle"  "$VIEW_REPORTS"  "$R_HELP"   \
      "VOYEUR"           "Toggle"  "$VOYEUR"        "$V_HELP"   \
      "REAP"             "Toggle"  "$REAP"          "$E_HELP"   \
      "TMPFS"            "Toggle"  "$TMPFS"         "$D_HELP"   \
      "VERBOSE"          "Toggle"  "$VERBOSE"       "$W_HELP"   \
      "ZLOCAL_OVERRIDES" "Toggle"  "$ZLOCAL_OVERRIDES" "$Z_HELP"`

  then
  
         ARCHIVE=off
   AUTORESURRECT=off
         AUTOFIX=off
       AUTOPRUNE=off
           CABAL=off
           COLOR=off
     KEEP_SOURCE=off    
    MAIL_REPORTS=off
        PRESERVE=off
           SOUND=off
         SUSTAIN=off
    VIEW_REPORTS=off
          VOYEUR=off
            REAP=off
         GARBAGE=off
           TMPFS=off
	 VERBOSE=off
ZLOCAL_OVERRIDES=off

    for  TOGGLE  in  $TOGGLES;  do
      case  $TOGGLE  in
             ARCHIVE)       ARCHIVE=on  ;;
       AUTORESURRECT) AUTORESURRECT=on  ;;
             AUTOFIX)       AUTOFIX=on  ;;
           AUTOPRUNE)     AUTOPRUNE=on  ;;
               CABAL)         CABAL=on  ;;
               COLOR)         COLOR=on  ;;
         KEEP_SOURCE)   KEEP_SOURCE=on  ;;      
        MAIL_REPORTS)  MAIL_REPORTS=on  ;;
            PRESERVE)      PRESERVE=on  ;;
               SOUND)         SOUND=on  ;;
             SUSTAIN)       SUSTAIN=on  ;;
        VIEW_REPORTS)  VIEW_REPORTS=on  ;;
              VOYEUR)        VOYEUR=on  ;;
                REAP)          REAP=on  ;;
             GARBAGE)       GARBAGE=on  ;;
               TMPFS)         TMPFS=on  ;;
	     VERBOSE)       VERBOSE=on  ;;
    ZLOCAL_OVERRIDES) ZLOCAL_OVERRIDES=on ;;
      esac
    done

    TEMP=`cat       $LOCAL_CONFIG    |
          grep  -v  "ARCHIVE="       |
	  grep  -v  "AUTORESURRECT=" |
          grep  -v  "AUTOFIX="       |
          grep  -v  "AUTOPRUNE="     |
          grep  -v  "CABAL="         |
          grep  -v  "KEEP_SOURCE="   |
          grep  -v  "MAIL_REPORTS="  |
          grep  -v  "VIEW_REPORTS="  |
          grep  -v  "PRESERVE="      |
          grep  -v  "SOUND="         |
          grep  -v  "SUSTAIN="       |
          grep  -v  "VOYEUR="        |
          grep  -v  "REAP="          |
          grep  -v  "GARBAGE="       |
          grep  -v  "color"          |
          grep  -v  "TMPFS="         |
	  grep  -v  "VERBOSE="       |
	  grep  -v  "ZLOCAL_OVERRIDES="`

    echo  "$TEMP"                        >   $LOCAL_CONFIG
    echo  "      ARCHIVE=$ARCHIVE"       >>  $LOCAL_CONFIG
    echo  "AUTORESURRECT=$AUTORESURRECT" >>  $LOCAL_CONFIG
    echo  "      AUTOFIX=$AUTOFIX"       >>  $LOCAL_CONFIG
    echo  "    AUTOPRUNE=$AUTOPRUNE"     >>  $LOCAL_CONFIG
    echo  "        CABAL=$CABAL"         >>  $LOCAL_CONFIG
    echo  "  KEEP_SOURCE=$KEEP_SOURCE"   >>  $LOCAL_CONFIG
    echo  " MAIL_REPORTS=$MAIL_REPORTS"  >>  $LOCAL_CONFIG
    echo  "     PRESERVE=$PRESERVE"      >>  $LOCAL_CONFIG
    echo  "        SOUND=$SOUND"         >>  $LOCAL_CONFIG
    echo  "      SUSTAIN=$SUSTAIN"       >>  $LOCAL_CONFIG
    echo  " VIEW_REPORTS=$VIEW_REPORTS"  >>  $LOCAL_CONFIG
    echo  "       VOYEUR=$VOYEUR"        >>  $LOCAL_CONFIG
    echo  "         REAP=$REAP"          >>  $LOCAL_CONFIG
    echo  "      GARBAGE=$GARBAGE"       >>  $LOCAL_CONFIG
    echo  "        color $COLOR"         >>  $LOCAL_CONFIG
    echo  "        TMPFS=$TMPFS"         >>  $LOCAL_CONFIG
    echo  "      VERBOSE=$VERBOSE"       >>  $LOCAL_CONFIG
    echo  "ZLOCAL_OVERRIDES=$ZLOCAL_OVERRIDES" >> $LOCAL_CONFIG
  fi
}


option_menu()  {
  while
    P_HELP="Enter the default delay time for prompts"
    E_HELP="Enter the email address for this box's administrator"
    F_HELP="Select many options on or off at once"
    M_HELP="Select ftp and http mirrors for faster downloads"
    O_HELP="Select architecture optimizations"
    I_HELP="Select the test that lin --fix, and AUTOFIX execute"
    D_HELP="Options for downloads"
    L_HELP="Select default lunar module"

    COMMAND=`$DIALOG  --title "Option Menu"                    \
                      --item-help                              \
                      --ok-label      "Select"                 \
                      --cancel-label  "Exit"                   \
                      --menu                                   \
                      ""                                       \
                      0 40 8                                   \
                      "L"  "Default Lunar Module"   "$L_HELP"  \
                      "P"  "Prompt Delay"           "$P_HELP"  \
                      "D"  "Download Options"       "$D_HELP"  \
                      "E"  "Admin's Email"          "$E_HELP"  \
                      "F"  "Feature Menu"           "$F_HELP"  \
                      "I"  "Integrity Checking"     "$I_HELP"  \
                      "M"  "Software Mirrors"       "$M_HELP"  \
                      "O"  "Optimize Architecture"  "$O_HELP"`

  do
    case  $COMMAND in
      P)  set_delay              ;;
      D)  download_options       ;;
      E)  set_email              ;;
      F)  feature_menu           ;;
      I)  integrity_menu         ;;
      M)  mirror_menu            ;;
      O)  optimize_architecture  ;;
      L)  set_default_module     ;;
    esac
  done
}


goodbye() {
  echo  "Have a good day."
  exit
}


background_execute() {
  $DIALOG  --msgbox  "Processing queues in the background."  0 0

  [  -f         $REMOVE_QUEUE  ]  &&
  lrm  `cat  $REMOVE_QUEUE`   1>/dev/null 2>&1
  rm  -f        $REMOVE_QUEUE

  if  [  -f            $INSTALL_QUEUE  ];  then
    lin  --deps  `cat  $INSTALL_QUEUE`
    (  lin       `cat  $INSTALL_QUEUE` 1>/dev/null 2>&1  &&
         rm -f         $INSTALL_QUEUE
    )  &
  fi

  goodbye
}


foreground_execute() {
  [  -f         $REMOVE_QUEUE  ]  &&
  lrm  `cat  $REMOVE_QUEUE`
  rm  -f        $REMOVE_QUEUE

  [  -f         $INSTALL_QUEUE  ]  &&
  $LIN   `cat  $INSTALL_QUEUE`
  rm  -f        $INSTALL_QUEUE

  goodbye
}


main_menu()  {
  while
    M_HELP="Easy module management"
    O_HELP="Change lunar options and features"
    L_HELP="View and edit lunar genreated log files."
    Q_HELP="View and edit lunar queues."
    F_HELP="Process queues while you watch and wait"
    N_HELP="Discover and fix internal problems"
    B_HELP="Process queues in the background"
    E_HELP="Exit without processing queues"
        OK="Select"
    CANCEL="Exit"

    if  [  "$CABAL"  ==  "on"  ];  then
      COMMAND=`$DIALOG --title "Main Menu"                   \
                       --item-help                           \
                       --ok-label      "$OK"                 \
                       --cancel-label  "$CANCEL"             \
                       --menu                                \
                       "$LUNAR_MODULE version: $LUNAR_VERSION"       \
                       0 0 0                                 \
                       "M"  "Module      Menu"     "$M_HELP"  \
                       "O"  "Option      Menu"     "$O_HELP"  \
                       "L"  "Log         Menu"     "$L_HELP"  \
                       "Q"  "Queue       Menu"     "$Q_HELP"  \
		       "N"  "Maintenance Menu"     "$N_HELP"  \
                       "F"  "Foreground  Execute"  "$F_HELP"  \
                       "B"  "Background  Execute"  "$B_HELP"`  
    else
      COMMAND=`$DIALOG --title "Main Menu"                   \
                       --item-help                           \
                       --ok-label      "$OK"                 \
                       --cancel-label  "$CANCEL"             \
                       --menu                                \
                       "$LUNAR_MODULE version: $LUNAR_VERSION"       \
                       0 0 0                                 \
                       "M"  "Module      Menu"     "$M_HELP"  \
                       "O"  "Option      Menu"     "$O_HELP"  \
                       "L"  "Log         Menu"     "$L_HELP"  \
                       "Q"  "Queue       Menu"     "$Q_HELP"  \
		       "N"  "Maintenance Menu"     "$N_HELP"  \
                       "F"  "Foreground  Execute"  "$F_HELP"  \
                       "B"  "Background  Execute"  "$B_HELP"`
    fi
  do
    case  $COMMAND in
      M)  module_menu         ;;
      O)  option_menu         ;;
      L)  log_menu            ;;
      Q)  queue_menu          ;;
      N)  maintenance_menu    ;;
      F)  foreground_execute  ;;
      B)  background_execute  ;;
    esac
  done
}



DIALOG="dialog
--backtitle
Lunar Module Management Utility
--stdout"

main() {
  #lets load the menu code
  if [ -n "$MENUS" ]; then
    for FILE in $(echo $MENUS/*.menu) ; do
      [ -s "$FILE" ] && . "$FILE"
    done
  fi

  export IFS="$TAB_ENTER_IFS"

  LUNAR_MODULE=${LUNAR_MODULE:=lunar}
  LUNAR_VERSION=$(run_details $LUNAR_MODULE && echo $UPDATED)

  main_menu
}
  
. /etc/lunar/config

GETOPT_ARGS=$(getopt -q -n lunar -o "dhv" -l "debug,help,verbose" -- "$@")

if [ -z "$?" ] ; then
  help | view_file
  exit
else
  eval set -- $GETOPT_ARGS
  root_check 
  enviro_check 
  set_priority

  while true ; do
    case "$1" in
      -d|--debug       ) (( LUNAR_DEBUG++ )) ; export LUNAR_DEBUG ; shift   ;;
      -h|--help        ) help ;                                     exit 1  ;;
      -v|--verbose     ) export VERBOSE="on" ;                      shift   ;;
      --) shift ; break ;;
      *) help ; break ;;
    esac
  done

  case "$1" in
    prune      ) prune                                  ;;
    renew      ) renew                                  ;;
    update     ) update                                 ;;
    rebuild    ) rebuild                                ;;
    optimize   ) optimize_architecture                  ;;
    fix        ) shift && run_fix $@                    ;;
    nofix      ) shift && export NOFIX=on && run_fix $@ ;;
    fixdepends ) shift && fix_depends $@                ;;
    resurrect  ) shift && resurrect_modules $@          ;;
    install    ) shift && lin $@                        ;;
    remove     ) shift && lrm $@                        ;;
    hold       ) shift && hold_modules $@               ;;
    unhold     ) shift && unhold_modules $@             ;;
    exile      ) shift && exile_modules $@              ;;
    unexile    ) shift && unexile_modules $@            ;;
    *) main ;; 
  esac
fi

