Advertisement
zfkhole

helper.tool

Oct 3rd, 2014
2,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.43 KB | None | 0 0
  1. #!/bin/bash
  2. shopt -s expand_aliases
  3. alias echo='echo -e'
  4. declare -i dupecheck=0
  5. creatediryn=""
  6. inun=""
  7. once="0"
  8. basejdir="/opt/java/current-java"
  9. ######
  10. # funcs
  11. ######
  12. usage() {
  13.     echo "#### this tool supplies the appropriate update-alternatives commands, it does NOT execute the commands for you"
  14.     echo "#### usage: $0 -i|-r [-heb]"
  15.     echo "#### \t -h|--help\t\t show this"
  16.     echo "#### \t -e|--examples\t\t show some examples"
  17.     echo "#### \t -i|--install\t\t provide the installation commands"
  18.     echo "#### \t -r|--remove\t\t provide the removal commands"
  19.     echo "#### \t -b|--basedir /example/java/directory\t specify the base java directory (default: $basejdir)"
  20.  
  21. if [ "$#" -gt 0 ]; then
  22.     echo "####################"
  23.     echo "## helper.tool -i"
  24.     echo " Display java installation commands in terminal"
  25.     echo " This assumes that $basejdir is a symlink pointing to the jdk folder (for example /opt/java/jdk1.7.0_67)"
  26.  
  27.     echo "\n### helper.tool -i -b /opt/java/64/current-java"
  28.     echo " Display installation commands which use /opt/java/64/current-java as the base directory"
  29.     echo " ie: /opt/java/64/current-java could be a symlink which points to /opt/java/jdk1.7.0_67-amd64"
  30.  
  31.     echo "\n### helper.tool -i -b /home/`whoami`/java-installation"
  32.     echo " Display installation commands which use /home/`whoami`/java-installation as the base directory"
  33.     echo " ie: you have installed java into /home/`whoami`/java-installation"
  34.  
  35.     echo "\n## helper.tool -r"
  36.     echo " Display java removal commands in terminal"
  37.     echo " This assumes that $basejdir is a symlink pointing to a jdk folder such as /opt/java/jdk1.8"
  38. fi
  39.  
  40.     quit 0
  41. }
  42. quit() {
  43.     unalias echo
  44.     shopt -u expand_aliases
  45.     unset inun once basejdir creatediryn binlist jrebinlist i k dupecheck
  46.     exit $1
  47. }
  48.  
  49. # https://gist.github.com/cosimo/3760587
  50. OPTS=`getopt -o hirb:e --long help,install,remove,basedir,examples -n 'parse-options' -- "$@"`
  51. if [ $? != 0 ]; then echo "Failed parsing options..."; quit 1; fi
  52. eval set -- "$OPTS"
  53.  
  54. # parse options
  55. while true; do
  56.         case "$1" in
  57.         ( -h | --help )
  58.             usage
  59.             ;;
  60.         ( -i | --install )
  61.             ((dupecheck++))
  62.             inun="in"
  63.             ;;
  64.         ( -r | --remove )
  65.             ((dupecheck++))
  66.             inun="un"
  67.             ;;
  68.         ( -b | --basedir )
  69.             shift
  70.             basejdir="$1"
  71.             ;;
  72.         ( -e | --examples )
  73.             usage "e"
  74.             ;;
  75.         ( -- ) shift; break ;;
  76.         ( -* ) echo "$0 - unrecognized option: $1"; usage;;
  77.         ( * ) break ;;
  78.     esac
  79.     shift
  80. done
  81.  
  82. # did they choose a mode?
  83. #if [ -z "$inun" ]; then echo "please select either -i or -r\n"; usage; fi
  84. # the line above this effectively does the same thing but i feel like an integer comparison would be more efficient ... idk
  85. if [ $dupecheck -gt 1 ]; then echo "please select either -i or -r\n"; usage; fi
  86.  
  87. # check that the java base directory exists
  88. if [ ! -d "$basejdir" ]; then
  89.     echo -n "the java base directory $basejdir doesn't exist. create it? "
  90.     while true; do
  91.         read -p "[Y/n] " -e creatediryn
  92.         case $creatediryn in
  93.         [Yy]* )
  94.             echo -n "sudo mkdir -p $basejdir: "
  95.             sudo mkdir -p $basejdir
  96.             if [ ! -d $basejdir ]; then echo "i couldn't create $basejdir!"; quit 1; fi
  97.             break;;
  98.         [Nn]* ) exit;;
  99.         * ) ;;
  100.         esac
  101.     done
  102.  
  103.     #echo "you need to create the current-java symlink which points to the most recent jdk folder"
  104.     #echo "for example: sudo ln -s /opt/java/jdk1.7.0_67 /opt/java/current-java"
  105. fi
  106.  
  107. # does the jdk/jre directory structure exist?
  108. if [[ ! -d "$basejdir/bin" || ! -d "$basejdir/jre/bin" ]]; then echo "please extract the jdk/jre files into $basejdir"; quit 0; fi
  109.  
  110. # files in /opt/java/current-java/bin take precedence over those in jre/bin i think
  111. cd $basejdir/bin
  112. binlist="`/bin/ls -1 *`"
  113.  
  114. echo "######## system config for $basejdir/bin/"
  115. echo "###########################################"
  116. for i in $binlist; do
  117.     # im not sure about this one ... seems like it could be ambiguous
  118.     if [ "$i" == "ControlPanel" ]; then
  119.         echo "# the file $basejdir/bin/ControlPanel was ignored"
  120.         continue
  121.     fi
  122.  
  123.     # display the appropriate command (using either --install&--set or --remove)
  124.     if [ "$inun" == "in" ]; then
  125.         # handle .../lib/jexec manually
  126.         if [ "$once" == "0" ]; then
  127.             echo "update-alternatives --install /usr/bin/jexec jexec $basejdir/lib/jexec 1065 && sleep 1s"
  128.             echo "update-alternatives --set jexec $basejdir/lib/jexec && sleep 1s"
  129.             once="1"
  130.         fi
  131.  
  132.         # display upd-alt command
  133.         echo "update-alternatives --install /usr/bin/$i $i $basejdir/bin/$i 1065 && sleep 1s"
  134.         echo "update-alternatives --set $i $basejdir/bin/$i && sleep 1s"
  135.     elif [ "$inun" == "un" ]; then
  136.         # handle .../lib/jexec manually
  137.         if [ "$once" == "0" ]; then
  138.             echo "update-alternatives --remove jexec $basejdir/lib/jexec && sleep 1s"
  139.             once="1"
  140.         fi
  141.  
  142.         # display upd-alt command
  143.         echo "update-alternatives --remove $i $basejdir/bin/$i && sleep 1s"
  144.     fi
  145. done
  146.  
  147.  
  148.  
  149. echo "######## system config for $basejdir/jre/bin/"
  150. echo "###########################################"
  151. cd $basejdir/jre/bin
  152. jrebinlist="`/bin/ls -1 *`"
  153.  
  154. for i in $jrebinlist; do
  155.     # im not sure about this one ... seems like it could be ambiguous
  156.     if [ "$i" == "ControlPanel" ]; then
  157.         echo "# the file $basejdir/bin/ControlPanel was ignored"
  158.         continue
  159.     fi
  160.  
  161.     # jexec has already been added/removed
  162.     if [ "$i" == "jexec" ]; then continue; fi
  163.  
  164.     #dont show commands for files that have already been printed
  165.     # (the files in .../bin/ and .../jre/bin are duplicate. you can confirm this by md5sum'ing them and comparing the result)
  166.     for k in $binlist; do
  167.                 if [ "$i" == "$k" ]; then
  168.             #echo "# skipping duplicate file: $i / $k"
  169.             skip="y"
  170.             break
  171.         fi
  172.         skip="n"
  173.     done
  174.  
  175.     # non-duplicate file ... print command
  176.     if [ "$skip" == "n" ]; then
  177.         if [ "$inun" == "in" ]; then
  178.             echo "update-alternatives --install /usr/bin/$i $i $basejdir/jre/bin/$i 1065 && sleep 1s"
  179.             echo "update-alternatives --set $i $basejdir/jre/bin/$i && sleep 1s"
  180.             skip="n"
  181.         elif [ "$inun" == "un" ]; then
  182.             echo "update-alternatives --remove $i $basejdir/jre/bin/$i && sleep 1s"
  183.         fi
  184.     fi
  185. done
  186.  
  187. echo "\n# finished! here's how you can use them:"
  188. echo "#\t1. copy the commands into a textfile called updatesystem.txt \n"
  189. echo "#\t2. save the textfile into your home folder like this: /home/`whoami`/updatesystem.txt"
  190. echo "#\t3. open a terminal and execute this command:"
  191. echo "#\t       chmod +x /home/`whoami`/updatesystem.txt"
  192. echo "#\t4. execute this command:"
  193. echo "#\t       /bin/bash /home/`whoami`/updatesystem.txt"
  194. echo "#\t\t Java is now installed!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement