#!/bin/bash
#Webmin Install Script written by Andrew Reis
#Copyright 2010 DBMS Inc.
#This script will fetch the Webmin.tar.gz file
#from the DBMS Inc. FTP site
#http://www.dbmsinc.com/webmin/webmin-1.620.tar.gz
#then it will extract the archive, create a tmp directory
#and install the program
#
#Initial creation
#Changed the install directory from /tmp/webmin to /keypos/webmin* - ASR 13 Sept, 2010
#Changed wget command to retrieve new version - ASR 03 Dec, 2010
#Changed wget to retrieve newest version - ASR 05 Aug, 2011
#

startDir=`pwd`;

#make the /keypos directory if it doesn't exist
mkdir /keypos -p;

#Change to the /keypos directory, prepare for download
cd /keypos;

#Remove Webmin is installed via yum
clear;
echo 'Checking for Webmin Repository file in /etc/yum.repos.d';
if [ -s /etc/yum.repos.d/webmin.repo ]; then 
echo 'Removing Webmin Repo file';
rm /etc/yum.repos.d/webmin.repo;
else echo "Webmin repo file not found";
read -p "Press any key to continue...";
fi;

#Dump RPM manifest out to file
echo 'Checking for installed Webmin RPM package';
if [ $(rpm -qa | grep 'webmin') ]; then
echo 'Removing Webmin RPM';
yum -y erase webmin;
else echo 'Webmin RPM Not installed';
read -p "Press any key to continue..."
fi;

#Execute wget to retrieve the tar package
echo 'Downloading Webmin Source';
# wget http://www.webmin.com/download/webmin-current.tar.gz
wget https://sourceforge.net/projects/webadmin/files/webmin/1.920/webmin-1.920-1.noarch.rpm

#Parse Version number from packagename
echo 'Searching for package version';
ver=(`ls webmin-* | tr '-' ' ' | tr '.' ' '`);
ver=${ver[1]}.${ver[2]};
echo "Current version is: $ver";
read -p "Press any key to continue..."

#Perform the tar command to uncompress the archive
echo 'Extracting Webmin Source';
tar -xzvf webmin-$ver.tar.gz

#Remove the Webmin.tar.gz package
rm webmin-$ver.tar.gz

#Changes to the newly extracted directory
cd ./webmin-$ver;

#Invoke the command line setup of Webmin in a Perl script
su -c "sh ./setup.sh";
cd $startDir;
rm $startDir/webmin.sh -f